2016-04-25 121 views
0

我使用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 

雖然我敢肯定我的疑問是正確的,我手動對它們進行測試。我該如何解決這個問題?

+1

一次嘗試一個聲明。 ';'對人類來說很方便,大多數數據庫連接器都不使用它。 – tadman

+0

如果我應該將每行放入不同的connection.query,您認爲它是一個更好的主意嗎? @tadman – abedzantout

+0

我認爲如果你的代碼能夠正常工作,這是一個更好的主意,這就是你使它工作的方式。 – tadman

回答

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`; 

所以它只有一個語句

相關問題