nodejs操作数据库简单示例
- npm init
- npm i mysql -S
let mysql = require('mysql') //当前目录下
let db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'web2022'
});
db.connect();
db.query('insert into major (name,age) values ("信息管理","45")')
db.query('SELECT * FROM major', function (error, results, fields) {
if (error) throw error;
console.log(results);
});
- 运行文件