Node.js mySQL module error

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'

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

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

mysql.createClient({});

but with

mysql.createConnection({});

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