安装: npm install -- save mysql
e.g(demo1.js):
//加载mysql包 var mysql = require('mysql'); //创建连接 var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '123456', database: 'user' }) //连接数据库 connection.connect() //操作数据库 connection.query(`insert into users values(null,"admin","123456")`, function (error, results, fields) { if (error) throw error console.log('The solution is:', results) }) //关闭连接 connection.end()1.打开数据库连接
2.新建数据库
3.操作数据库
输入命令:nodemon demo1.js,运行上面的代码
运行:
connection.query(`select * from users`, function (error, results, fields) { if (error) throw error console.log('The solution is:', results) })ps:由于我插入了两次,所以有两条一样的数据
连接成功!
4.关闭连接
connection.end( );