1

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!

2
  • The database 201803028 does not exist. You should create it before you connect to it. Commented Mar 28, 2018 at 8:14
  • 2
    Is 201803028 correct? It might be 20180328 Commented Mar 28, 2018 at 8:14

1 Answer 1

0

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);
});*/
Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide some explanation in addition to the code example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.