我正在做一个节点js项目来创建数据库并在其中添加一些种子 但是我得到了:
错误:ER_PARSE_ERROR:您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以在第1行的')'附近使用正确的语法
const mysql = require('mysql');
const bcrypt = require('bcrypt');
const mysql_co = mysql.createConnection({
host: "localhost",
user: "root",
password: "rootetna"
});
mysql_co.connect(function(err) {
if (err) throw err;
mysql_co.query("DROP DATABASE IF EXISTS skigroo");
mysql_co.query("CREATE DATABASE skigroo", function (err, result) {
if (err) throw err;
console.log("Database \"skigroo\" created.");
mysql_co.query("USE skigroo");
bcrypt.genSalt(10, function(errB, salt) {
if (errB) throw errB;
bcrypt.hash("hashtem" ,salt , function(errC, hash) {
if (errC) throw errC;
mysql_co.query("CREATE TABLE groups(`group_id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(45) NOT NULL,PRIMARY KEY (`group_id`))ENGINE = InnoDB");
mysql_co.query("CREATE TABLE users(`user_id` INT NOT NULL AUTO_INCREMENT,`username` VARCHAR(45) NOT NULL,`email` VARCHAR(45) NOT NULL,`password` VARCHAR(45) NOT NULL,`phone_number` INT NOT NULL,`age` INT NOT NULL,`level` VARCHAR(45) NOT NULL,`acess_token` VARCHAR(1300) NULL,`groups_group_id` INT NOT NULL,PRIMARY KEY (`user_id`, `groups_group_id`),UNIQUE INDEX `username_UNIQUE` (`username` ASC),UNIQUE INDEX `email_UNIQUE` (`email` ASC),INDEX `fk_users_groups1_idx` (`groups_group_id` ASC),CONSTRAINT `fk_users_groups1`FOREIGN KEY (`groups_group_id`)REFERENCES `skigroo`.`groups` (`group_id`)ON DELETE NO ACTION ON UPDATE NO ACTION)ENGINE = InnoDB");
mysql_co.query("CREATE TABLE events(`event_id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(45) NOT NULL,`description` VARCHAR(45) NOT NULL,`groups_group_id` INT NOT NULL,PRIMARY KEY (`event_id`, `groups_group_id`),INDEX `fk_events_groups1_idx` (`groups_group_id` ASC),CONSTRAINT `fk_events_groups1`FOREIGN KEY (`groups_group_id`)REFERENCES `skigroo`.`groups` (`group_id`)ON DELETE NO ACTION ON UPDATE NO ACTION)ENGINE = InnoDB;");
mysql_co.query("CREATE TABLE friends(`friends_id` INT NOT NULL AUTO_INCREMENT,`accepted` TINYINT(1) NULL,`users_user_id` INT NOT NULL,PRIMARY KEY (`friends_id`, `users_user_id`),INDEX `fk_Friends_users_idx` (`users_user_id` ASC),CONSTRAINT `fk_Friends_users`FOREIGN KEY (`users_user_id`)REFERENCES `skigroo`.`users` (`user_id`)ON DELETE NO ACTION ON UPDATE NO ACTION)ENGINE =InnoDB;", (err, rows, field) => {
if (err) throw err;
console.log('Tables "users", "events" , "groups" and "friends" created.');
mysql_co.query('INSERT INTO groups (name) VALUES ("grouptest1");');
console.log("ça passe");
mysql_co.query('INSERT INTO groups (name) VALUES ("grouptest2");');
console.log("ça passe");
mysql_co.query('INSERT INTO groups (name) VALUES ("grouptest3");');
console.log("ça passe");
mysql_co.query('INSERT INTO users (username, email, password, phone_number, age, level) VALUES ("Valem", "test@test.test", ? , "0956321345" , "1" , "12", );',[hash] );
mysql_co.query('INSERT INTO users (username, email, password, phone_number, age, level) VALUES ("stenvo", "stenvo_v@etna-alternance.net", ? , "0912121212" ,"4","14", );',[hash] );
mysql_co.query('INSERT INTO users (username, email, password, phone_number, age, level) VALUES ("lottin", "lottin_s@etna-alternance.net", ? , "09143433" ,"9","24", );',[hash] );
mysql_co.query('INSERT INTO users (username, email, password, phone_number, age, level) VALUES ("emery", "emery_a@etna-alternance.net", ? , "12345678" ,"123","99", );',[hash] );
console.log("bug");
mysql_co.query('INSERT INTO events (name, description) VALUES ("test1", "event1");');
console.log("bug");
mysql_co.query('INSERT INTO events (name, description) VALUES ("test2", "event3");');
console.log("OUI");
mysql_co.query('INSERT INTO friends (accepted) VALUES ("1")');
console.log("non");
mysql_co.end();
});
});
});
});
});
错误输出为错误:
ER_PARSE_ERROR:您的SQL语法有错误;查看手册 对应于您的MariaDB服务器版本的正确语法 在第1行的')'附近使用
找不到原因。
答案 0 :(得分:0)
导入MySQL软件包。
var mysql = require('mysql');
答案 1 :(得分:0)
First import MySql library
const mysql = require('mysql');
And second instead of running multiple insert statement, run single command as below.
var sql = "INSERT INTO groups (name) VALUES ?";
var values = [
['grouptest1'],['grouptest2'],['grouptest3']
];
mysql_co.query(sql, [values], function(err) {
if (err) throw err;
});