0

Playing with mysql module and code examples from Packt’s Node Cookbook.

var mysql = require('mysql');
var client = mysql.createClient({
user: 'root',
password: 'sqlpassword',
//debug: true  
});

var ignore = [mysql.ERROR_DB_CREATE_EXISTS,
          mysql.ERROR_TABLE_EXISTS_ERROR];

client.on('error', function (err) {
if (ignore.indexOf(err.number) + 1) { return; }
throw err;
});

client.query('CREATE DATABASE quotes');
client.useDatabase('nodb');
client.query('CREATE TABLE quotes.quotes (' +
         'id INT NOT NULL AUTO_INCREMENT,' +
         'author VARCHAR( 128 ) NOT NULL,' +
         'quote TEXT NOT NULL, PRIMARY KEY (  id )' +
         ')');

client.query('INSERT INTO  quotes.quotes (' +
          'author, quote) ' +
          'VALUES ("Proof by analogy is fraud.", "Bjarne Stroustrup");');

client.end();

and that code return me error Object # has no method 'useDatabase'

1

3 Answers 3

3

I beleive you want to specify the database when connecting.

var mysql = require('mysql');
var client = mysql.createClient({
user: 'root',
password: 'sqlpassword',
database: 'nodb'
//debug: true  
});

var ignore = [mysql.ERROR_DB_CREATE_EXISTS,
          mysql.ERROR_TABLE_EXISTS_ERROR];

client.on('error', function (err) {
if (ignore.indexOf(err.number) + 1) { return; }
throw err;
});

client.query('CREATE DATABASE quotes');
client.query('CREATE TABLE quotes.quotes (' +
         'id INT NOT NULL AUTO_INCREMENT,' +
         'author VARCHAR( 128 ) NOT NULL,' +
         'quote TEXT NOT NULL, PRIMARY KEY (  id )' +
         ')');

client.query('INSERT INTO  quotes.quotes (' +
          'author, quote) ' +
          'VALUES ("Proof by analogy is fraud.", "Bjarne Stroustrup");');

client.end();
Sign up to request clarification or add additional context in comments.

1 Comment

I doesn't think so, because this code 'client.query('CREATE DATABASE quotes');' create new base.
1

migration node-mysql v0.9 example to v2.0. https://github.com/felixge/node-mysql

var mysql = require('mysql');

var client = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'OURPASSWORD'
  //debug: true
});

var ignore = [mysql.ERROR_DB_CREATE_EXISTS,
              mysql.ERROR_TABLE_EXISTS_ERROR];

client.on('error', function (err) {
  if (ignore.indexOf(err.number) + 1) { return; }
  throw err;
});

client.query('CREATE DATABASE quotes');
client.query('USE quotes');

client.query('CREATE TABLE quotes.quotes (' +
             'id INT NOT NULL AUTO_INCREMENT,' +
             'author VARCHAR( 128 ) NOT NULL,' +
             'quote TEXT NOT NULL, PRIMARY KEY (  id )' +
             ')');

client.query('INSERT INTO  quotes.quotes (' +
              'author, quote) ' +
              'VALUES ("Bjarne Stroustrup", "Proof by analogy is fraud.");');

client.end();

Comments

0

In newest version of this module connection to mySQL established not by

mysql.createClient({});

but with

mysql.createConnection({});

Comments

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.