# SQLite NodeJS
var fs = require("fs");
var file = "./db/test2.db"; //这里写的就是数据库文件的路径
var exists = fs.existsSync(file);
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
db.run("DROP TABLE COMPANY");
db.run(`CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
)`);
//增
var sql1 = db.prepare(`
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
`);
sql1.run();
//删
var sql2 = db.prepare("DELETE FROM COMPANY where id = 2");
sql2.run();
//改
var sql3 = db.prepare("UPDATE COMPANY SET NAME = Paul where id = 1");
sql3.run();
//查
//查一个表的所有数据
db.all("select * from COMPANY", function (err, row) {
console.log(JSON.stringify(row));
});
//查一条数据
db.each("select * from COMPANY", function (err, row) {
console.log(row);
});