如何通过Node从MySQL查询数据

我是Node的新手,学习如何从MySQL查询数据 . 我收到以下错误:

err {[错误:ER_BAD_DB_ERROR:未知数据库'201803028']代码:'ER_BAD_DB_ERROR',错误号:1049,sqlMessage:'未知数据库'201803028 '',sqlState:'42000',致命:true}

这是我的代码

const mysql = require('mysql'); var db = mysql.createConnection({host:'localhost',user:'root',password:'123456',database:'201803028'}); db.query('SELECT * FROM user-table;',(err,data)=> {if(err){console.log('err',err);} else {console.log('success',data );}})

谁能告诉我我的编码有什么问题?

非常感谢你!

回答(1)

3 years ago

试试这个

var mysql = require('mysql')
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'yourpassword',
  database : 'yourdatabasename'
})

connection.connect();
/* Uncomment the below query to test your connction
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});*/