2016-06-18 59 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