我试图在启动时使用虚拟数据填充mysql数据库,但我一直遇到这个问题
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 'INSERT INTO Teachers
我试着检查一下这种语法是否正确,大多数在线检查员都告诉我这没有什么问题。但尽管如此,它给了我这个错误。但是,如果我尝试通过mysql workbench查询数据库,它可以正常工作并插入数据而没有任何问题。
const mysql = require('mysql')
const config = require('./config')
var con = mysql.createConnection({
host: config.db.options.host,
user: config.db.user,
password: config.db.password
})
let superScript = `
USE school;
INSERT INTO Teachers
(FirstName,LastName,Gender,Address,PhoneNumber,Email ,Facebook_ID
,Qualification, Good_at,Personal_Description,Teachering_Experience,Music_skill,Language_skill,Image_URL)
VALUES("Glennys","Looker","Male","125 Melbourne street",
"1234567890","abc123@gmail.com"
,"GlennysFB","Master","Piano"
,"I am a pianist and composer who has taught people of all ages and levels, including absolute beginners, adult students, and taking students through AMEB or VCE exams. "
," 7 Years Teaching Experience","Piano","English,Japanese"
,"https://segundadivisionweb.files.wordpress.com/2017/04/profesor- de-musica.jpg");`;
function createDB () {
con.connect(function (err) {
if (err) throw err
console.log('Connected!')
con.query(`CREATE DATABASE IF NOT EXISTS ${config.db.database}`,
function (err, result) {
if (err) throw err
console.log('Database created')
})
con.query(superScript, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
})
}
我该怎么做才能插入这些数据?
答案 0 :(得分:0)
不是程序员,而是AFAIK,您不能在其中发出多个语句。
尝试这样(没有USE声明):
let superScript = `
INSERT INTO school.Teachers
或将数据库放入连接设置。
答案 1 :(得分:0)
如果有人遇到同样的问题,这就是解决方案
var con = mysql.createConnection({
host: config.db.options.host,
user: config.db.user,
password: config.db.password,
database: config.db.database,
multipleStatements: true
})
这将允许你在1个字符串中插入尽可能多的元素