【发布时间】:2016-10-20 03:12:11
【问题描述】:
s = "CREATE TABLE " + tableName +"\n" +
"(\n" +
" " + tablelower + "_currentid INT PRIMARY KEY AUTO_INCREMENT,\n" +
" " + tablelower + "_id VARCHAR(8) NOT NULL,\n" +
" " + tablelower + "_name VARCHAR(45) NOT NULL,\n" +
" " + tablelower + "_type VARCHAR(45) NOT NULL,\n" +
" " + tablelower + "_topic VARCHAR(255) NOT NULL,\n" +
" " + tablelower + "_pin VARCHAR(6) NOT NULL,\n" +
" " + tablelower + "_device VARCHAR(100) NOT NULL,\n" +
" " + tablelower + "_device_id INT NOT NULL,\n" +
" FOREIGN KEY(" + tablelower + "_device_id) REFERENCES Devices(device_currentid)\n" +
");\n" +
"\n" +
" delimiter | \n" +
" CREATE TRIGGER " + tablelower + "_trigger BEFORE INSERT ON " + tableName +
" FOR EACH ROW\n" +
" BEGIN\n" +
" SET new." + tablelower + "_id = CONCAT('" + topic + "',LPAD((SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '" + tableName + "'),4,'0'));\n" +
" SET new." + tablelower + "_topic = CONCAT((SELECT device_topic FROM Devices WHERE device_name LIKE new." + tablelower + "_device),'/',(new." + tablelower + "_id));\n" +
" END;\n" +
" | \n" +
" delimiter ;";
mysqlconn.createStatement().execute(s);
上面是代码,它将使用给定的名称为 tableName 和 tablelower 创建一个表和一个触发器,它们是字符串变量。这是我写的第一个版本,我收到以下错误:
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 'delimiter |
CREATE TRIGGER tablename_trigger BEFORE INSERT ON tablename FO' at line 14
在 google 帮助后,我找到了这个线程 Error while creating trigger through JDBC on mysql5.5 和文档 http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html,我已经将我的代码更改为 StringBuilder:
tableCreation.append("CREATE TABLE " + tableName);
tableCreation.append("(");
tableCreation.append(tablelower + "_currentid INT PRIMARY KEY AUTO_INCREMENT,");
tableCreation.append(tablelower + "_id VARCHAR(10) NOT NULL,");
tableCreation.append(tablelower + "_name VARCHAR(45) NOT NULL,");
tableCreation.append(tablelower + "_type VARCHAR(45) NOT NULL,");
tableCreation.append(tablelower + "_topic VARCHAR(255) NOT NULL,");
tableCreation.append(tablelower + "_pin VARCHAR(6) NOT NULL,");
tableCreation.append(tablelower + "_device VARCHAR(100) NOT NULL,");
tableCreation.append(tablelower + "_device_id INT NOT NULL,");
tableCreation.append("FOREIGN KEY(" + tablelower + "_device_id) REFERENCES Devices(device_currentid)");
tableCreation.append(" ); ");
tableCreation.append("DELIMITER // ");
tableCreation.append(" CREATE");
tableCreation.append(" TRIGGER " + tablelower + "id_trigger ");
tableCreation.append(" BEFORE INSERT");
tableCreation.append(" ON " + tableName + " FOR EACH ROW");
tableCreation.append(" BEGIN");
tableCreation.append(" SET new." + tablelower + "_id = CONCAT('" + topic + "',LPAD((SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '" + tableName + "'),4,'0'));");
tableCreation.append(" SET new." + tablelower + "_topic = CONCAT((SELECT device_topic FROM Devices WHERE device_name LIKE new." + tablelower + "_device),'/',(new." + tablelower + "_id));");
tableCreation.append(" END;//");
tableCreation.append("DELIMITER ; ");
mysqlconn.createStatement().execute(tableCreation.toString());
但在这些更改之后,我仍然收到此错误:
您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 'DELIMITER // 在插入之前创建触发器 reedswitchid_trigger 在第 1 行打开 ReedSwitches。
我正在通过 java 执行此操作,并且 MySQL 服务器位于 Raspberry pi 2 上。 如需更多信息评论和注意我是 SQL 的初学者。谢谢
编辑:
您的 SQL 语法有错误;检查与>您的 MySQL 服务器版本相对应的手册,以在第 1 行的“SET >new.lightsensor_topic = CONCAT((SELECT device_topic FROM Devices WHERE >devic”附近使用正确的语法。退出
tabl = "CREATE TABLE " + tableName +
"(" + " " + tablelower + "_currentid INT PRIMARY KEY AUTO_INCREMENT, " +
tablelower + "_id VARCHAR(8) NOT NULL, " +
tablelower + "_name VARCHAR(45) NOT NULL, " +
tablelower + "_type VARCHAR(45) NOT NULL, " +
tablelower + "_topic VARCHAR(255) NOT NULL, " +
tablelower + "_pin VARCHAR(6) NOT NULL, " +
tablelower + "_device VARCHAR(100) NOT NULL, " +
tablelower + "_device_id INT NOT NULL, " +
"FOREIGN KEY(" + tablelower + "_device_id) REFERENCES Devices(device_currentid)" +
")";
trigg=
" CREATE TRIGGER " + tablelower + "_trigger BEFORE INSERT ON " + tableName +
" FOR EACH ROW" +
" SET new." + tablelower + "_id = CONCAT('" + topic + "',LPAD((SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '" + tableName + "'),4,'0'))" +
" SET new." + tablelower + "_topic = CONCAT((SELECT device_topic FROM Devices WHERE device_name LIKE new." + tablelower + "_device),'/',(new." + tablelower + "_id))" +
" END";
mysqlconn = data.getConnection();
mysqlconn.createStatement().execute(tabl);
mysqlconn.createStatement().execute(trigg);
【问题讨论】:
标签: java mysql syntax-error mysql-5.5