Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node readline解析并尝试将单个行发布到mysql_Mysql_Node.js_Readline_Fs - Fatal编程技术网

Node readline解析并尝试将单个行发布到mysql

Node readline解析并尝试将单个行发布到mysql,mysql,node.js,readline,fs,Mysql,Node.js,Readline,Fs,我试图使用node.js来处理MySQL数据库的.sql文件,但我的代码似乎在分别解析.sql的每一行,导致错误,指出第1行的语法中存在错误:重复 我尝试过使用StringDecoder,但没有成功,还有chunk.toString。我哪里做错了?我认为这是一个非常简单的场景。有没有更好的方法让节点执行.sql脚本 var mysql = require ('mysql'); var fs = require('fs'); var readline = require('readline');

我试图使用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中执行得很好,但如果我试图通过命令行和节点程序调用它,则会失败

片段:

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
错误1064(42000):您的SQL语法有错误;在第1行“/database/builddb.sql”附近,检查与MySQL服务器版本对应的手册,以获得正确的语法

请使用sqlUpdated@ŁukaszSzewczak粘贴您的文件