2016-06-18 66 views
1
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); 

以上是代碼,這將使一張桌子和一個觸發它與給定名稱表名和tablelower這是字符串變量。這是第一個版本,我寫,我是越來越以下錯誤:如何爲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 

此前谷歌的幫助,我發現這個線程Error while creating trigger through JDBC on mysql5.5和DOC 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服務器上Raspbe RRY 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); 

回答

0

的JDBC Stateent#executeXXX方法旨在執行 SQL語句,一次不能多條語句。
即使某些數據庫/驅動程序實現允許這一點,這是一個非標準的方式,它是不可移植的,所以根本就不使用它。

把你的代碼轉換成兩個語句:

String s1 = "CREATE TABLE (.......)"; 
String s2 = "CREATE TRIGGER .......END"; 
mysqlconn.createStatement().execute(s1); 
mysqlconn.createStatement().execute(s2); 

兩個重要注意事項:

  • 在SQL月底不要用分號;命令在JDBC
  • 不使用DELIMITER x命令 - 這是不是SQL命令,但MySQL的客戶端命令,數據庫不recogize它
+0

Ø k我現在會試試謝謝:) – HarisJMD