为什么mysql给我一个错误?
问题描述:
我使用Node.js和node-mysql来执行我的查询。 这里是我使用的代码:为什么mysql给我一个错误?
var connection = mysql.createConnection(dbconfig.connection);
connection.query('CREATE DATABASE ' + dbconfig.database);
connection.query('USE '+ dbconfig.database);
connection.query('drop table if exists `ANSWER`; \
drop table if exists `ANSWERTYPE`; \
drop table if exists `COURSE`; \
drop table if exists `COURSEINSTANCE`; \
drop table if exists `ENROLL`; \
drop table if exists `FACULTY`; \
drop table if exists `QUESTION`; \
drop table if exists `QUESTIONGROUP`; \
drop table if exists `SEMESTER`; \
drop table if exists `STUDENTS`; \
drop table if exists `TEACHER`; \
........
然而节点不断显示错误:
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 'drop table if exists `ANSWERTYPE`; drop table if exists `COURSE`; drop table if ' at line 1
虽然我敢肯定我的疑问是正确的,我手动对它们进行测试。我该如何解决这个问题?
答
为DROP TABLE的语法是:
DROP [TEMPORARY] TABLE [IF EXISTS] [/*COMMENT TO SAVE*/]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
所以你可以把一个桌子后面下就好
drop table if exists `ANSWER`, `ANSWERTYPE`, `COURSE`, `COURSEINSTANCE`;
所以它只有一个语句
一次尝试一个声明。 ';'对人类来说很方便,大多数数据库连接器都不使用它。 – tadman
如果我应该将每行放入不同的connection.query,您认为它是一个更好的主意吗? @tadman – abedzantout
我认为如果你的代码能够正常工作,这是一个更好的主意,这就是你使它工作的方式。 – tadman