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
在谷歌帮助下我找到了这个线程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());
但在这些更改之后我仍然收到此错误:
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 reedswitchid_trigger BEFORE INSERT ON ReedSwitches' at line 1.
我正在通过 java 执行此操作,MySQL 服务器位于 Raspberry pi 2 上。 如需更多信息,请评论并注意我是 SQL 初学者。谢谢
编辑:
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 'SET >new.lightsensor_topic = CONCAT((SELECT device_topic FROM Devices WHERE >devic' at line 1. Exiting
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);
最佳答案
JDBC Stateent#executeXXX 方法旨在执行单个 SQL 语句,而不是同时执行多个语句。
即使某些数据库/驱动程序实现允许这样做,这也是一种非标准方式,并且不可移植,因此请不要使用它。
将您的代码分成两个语句:
String s1 = "CREATE TABLE ( ....... )";
String s2 = "CREATE TRIGGER .......END";
mysqlconn.createStatement().execute( s1 );
mysqlconn.createStatement().execute( s2 );
两个重要说明:
- 不要在 JDBC 中的 SQL 命令末尾使用分号
;
- 不要使用
DELIMITER x
命令——它不是SQL 命令,而是mysql-client 命令,数据库无法识别它
关于java - 如何为 mysql 5.5.44 创建触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895828/