Newer
Older
2022-autumn-study / database3.js
@ItoRino ItoRino on 16 Oct 2022 733 bytes add:db
//jsプログラムでテーブルを作成してデータを追加する
//http://www.nct9.ne.jp/m_hiroi/light/node02.html
const sqlite3 = require('sqlite3'),
      db = new sqlite3.Database('sample.sqlite'),
      data = [[1, 'Foo', 50, 'male', 'foo@yahoo.co.jp'],
              [2, 'Bar', 35, 'female', 'bar@yahoo.co.jp'],
              [3, 'Baz', 40, 'male', 'baz@yahoo.co.jp'],
              [4, 'Oops', 30, 'female', 'oops@yahoo.co.jp']];

db.serialize();
db.run("create table person (id integer, name text, age integer, sex text, email text)");

const sth = db.prepare("insert into person (id, name, age, sex, email) values (?,?,?,?,?)");
for (let xs of data) {
  sth.run(xs);
}
sth.finalize();

db.close();
console.log("OK");