我试图使用node.js来处理MySQL数据库的.sql文件,但我的代码似乎是单独解析.sql的每一行,导致错误表明我的错误第1行的语法:重复。
我试过使用StringDecoder,但没有运气,还有chunk.toString。我哪里错了?我认为这是一个非常简单的场景。是否有更好的方法让节点执行.sql脚本?
var mysql = require ('mysql');
var fs = require('fs');
var readline = require('readline');
var mysqlConnection = mysql.createConnection({
host: 'localhost',
port: '3306',
database: 'test',
user: 'test',
password: 'test'
});
mysqlConnection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
var rl = readline.createInterface({
input: fs.createReadStream('database/builddb.sql'),
terminal: true
});
rl.on('line', function(chunk){
mysqlConnection.query(chunk.toString('ascii'), function(err, sets, fields){
if(err) console.log(err);
});
});
rl.on('close', function(){
console.log("finished");
mysqlConnection.end();
});
当我按原样执行时,我的连接成功,但我收到错误,例如:
computer:code username$ node builddb.js
finished
Connected!
{ [Error: ER_EMPTY_QUERY: Query was empty]
code: 'ER_EMPTY_QUERY',
errno: 1065,
sqlState: '42000',
index: 0 }
{ [Error: ER_EMPTY_QUERY: Query was empty]
code: 'ER_EMPTY_QUERY',
errno: 1065,
sqlState: '42000',
index: 0 }
{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 1]
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0 }
{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'event_id VARCHAR(500) NOT NULL,' at line 1]
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0 }
{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'event_type VARCHAR(500) NOT NULL,' at line 1]
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0 }
编辑:SQL非常简单,只是创建表和插入数据。该脚本在MySQL Workbench中执行得很好,但如果我尝试通过命令行以及Node程序调用它,则会失败。
段:
create database if not exists testdb;
use testdb;
/** drop tables if they exist **/
drop table if exists alerts;
drop table if exists checkpoint;
drop table if exists docs;
/** create tables **/
CREATE TABLE alerts (
event_id VARCHAR(500) NOT NULL,
event_type VARCHAR(500) NOT NULL,
subscriber_id VARCHAR(2000) NOT NULL,
file_timestamp TIMESTAMP NOT NULL,
message_details VARCHAR(2000) NOT NULL,
notification_status VARCHAR(2000) NOT NULL DEFAULT 'PENDING'
CHECK (notification_status IN ('PENDING', 'SENT', 'EXCEPTION')),
created_by VARCHAR(2000) NOT NULL DEFAULT 'BATCH',
created_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (event_id, event_type));
CREATE TABLE checkpoint (
file_name VARCHAR(500) NOT NULL,
processing_status VARCHAR(2000) NOT NULL DEFAULT 'PENDING'
CHECK (processing_status IN ('PENDING', 'IN_PROCESS', 'COMPLETED', 'EXCEPTION')),
created_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_modified_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (file_name));
CREATE TABLE docs (
event_type VARCHAR(2000) NOT NULL,
msg_id VARCHAR(2000) NOT NULL,
doc_id VARCHAR(500) NOT NULL,
PRIMARY KEY (doc_id));
通过命令行执行时,它返回:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<filepath removed>/database/builddb.sql' at line 1