gpt4 book ai didi

mysql - Node.js mySQL 模块错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:54 26 4
gpt4 key购买 nike

使用 Packt 的 mysql 模块和代码示例 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();

并且该代码返回错误 Object # has no method 'useDatabase'

最佳答案

我相信你想在连接时指定数据库。

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

关于mysql - Node.js mySQL 模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14916777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com