首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js mySQL模块错误

Node.js mySQL模块错误
EN

Stack Overflow用户
提问于 2013-02-17 00:34:03
回答 3查看 3.1K关注 0票数 0

使用mysql模块和Packt的节点烹饪簿中的代码示例。

代码语言:javascript
复制
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();

这段代码返回了错误对象#没有方法'useDatabase'

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-17 03:50:32

我想您在连接时要指定数据库。

代码语言:javascript
复制
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();
票数 3
EN

Stack Overflow用户

发布于 2013-06-30 07:39:18

迁移节点-MySQLV0.9示例到v2.0。https://github.com/felixge/node-mysql

代码语言:javascript
复制
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();
票数 1
EN

Stack Overflow用户

发布于 2013-02-17 13:53:20

在这个模块的最新版本中,与mySQL的连接不是由

代码语言:javascript
复制
mysql.createClient({});

但是有了

代码语言:javascript
复制
mysql.createConnection({});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14916777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档