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


  错误[[错误:ER_BAD_DB_ERROR:未知数据库'201803028']
    代码:“ ER_BAD_DB_ERROR”,
    errno:1049,
    sqlMessage:'未知数据库\'201803028 \',
    sqlState:“ 42000”,
    致命的:真实的


这是我的代码


  const mysql = require('mysql');
  
  var db =
  mysql.createConnection({
     主机:“ localhost”,
     用户:“ root”,
     密码:“ 123456”,
     数据库:'201803028'});
  
  db.query('SELECT * FROM user-table;',(err,data)=> {
    如果(错误){
     console.log('err',err);
    }其他{
     console.log('成功',数据);
    }
   })


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

非常感谢你!

最佳答案

尝试这个

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);
});*/

10-06 04:54
查看更多