连接数据库
varmysql=require('mysql'); varconnection=mysql.createConnection({ host: 'localhost', user: 'root', password: '你的mysql密码', database: 'study'}); connection.connect();
增加数据
constdata= [2, "Java"] connection.query('INSERT INTO book(Id, Name) VALUES (?,?)', data, function (error, results, fields) { if (error) throwerror; console.log({ results }); });
查询数据
connection.query('SELECT * from book', function (error, results, fields) { if (error) throwerror; console.log({ results }); });
修改数据
constdata= ["Java8"] connection.query("UPDATE book SET Name = ? WHERE Id = 2", data, function (error, results, fields) { if (error) throwerror; console.log({ results }); });
删除数据
connection.query("DELETE FROM book WHERE Id = 2", function (error, results, fields) { if (error) throwerror; console.log({ results }); });