nodejs+mysql入门实例(表的查询)

时间:2023-03-09 17:32:34
nodejs+mysql入门实例(表的查询)
//连接数据库
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '******', //数据库地址
user: '******', //数据库用户名
password: '******', //数据库管理密码
database:'********', //数据库名称
insecureAuth: true
}); connection.connect(

connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}

console.log('connected as id ' + connection.threadId);
});

);
//查询
connection.query('select * from userinfo', function(err, rows, fields) {
if (err) throw err;
console.log('查询结果为: ', rows);
});
//关闭连接
connection.end();

  这里查询的表为userinfo,当然查询的时候,你的数据库里面必须得提前建立了这个表

nodejs+mysql入门实例(表的查询)