1
votes

I'm new to Node and learning how to query data from MySQL. I get the following error:

err { [Error: ER_BAD_DB_ERROR: Unknown database '201803028'] code: 'ER_BAD_DB_ERROR', errno: 1049, sqlMessage: 'Unknown database \'201803028\'', sqlState: '42000', fatal: true }

here is my code

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

Could anyone tell me what's the matter with my coding?

Thank you very much!

1
The database 201803028 does not exist. You should create it before you connect to it.Csaba
Is 201803028 correct? It might be 20180328Adder

1 Answers

0
votes

Try this

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