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에 주어진 이름으로 테이블과 트리거를 만드는 코드입니다. 이것은 내가 쓴 첫 번째 버전이며, 나는 아래의 오류가 발생되었습니다 여기에 나는이 스레드 Error while creating trigger through JDBC on mysql5.5과 문서 http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html를 발견하고 나는 모두 StringBuilder에 내 코드를 변경했습니다mysql 5.5.44에 대한 트리거를 만드는 방법은 무엇입니까?
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 도움말 후 :
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.
내가 자바를 통해이를 실행하고 있고 MySQL 서버가 Raspbe에 :
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());
는하지만 여전히 이러한 변경 후 나는이 오류 rry pi 2. 더 많은 정보 댓글과 에 대해 나는 SQL 초보자입니다. 감사
편집 : JDBC Stateent # executeXXX 방법이 단일 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);
O 나는 지금 감사 할 것입니다. :) – HarisJMD