2010-09-06 87 views
0

我正在使用MYSQL 5.1,我試圖在我的java類中編寫插入語句。這聽起來很簡單,但我試圖得到例外。我相信我的代碼有一個語法錯誤。下面是任何一個幫助我在這裏的片段。Mysql插入命令語法

  • ID是主鍵
  • atm_ID爲varchar
  • trasn_id爲varchar
  • SYSDATE是日期
  • 其餘爲int

請幫助我。

錯誤:

SQL statement is not executed!com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '£10, £20, £50, £100, trans_Id) VALUES (7,'hello','hello','2','2','2','2','2','he' at line 1`

代碼:

String query = "INSERT INTO atm_data (ID,atm_Id, sysDate, availBalance, £10, £20, £50, £100, trans_Id) VALUES (7,'hello','2010-09-15 01:20:06','2','2','2','2','2','hello')"; 
+2

我建議改變你的列名,使它們只包含字母數字字符和下劃線。雖然在技術上可以使用表名中的任何字符,但通常會導致問題並需要特殊處理。 – Mchl 2010-09-06 16:47:38

回答

1

它看起來像你只需要簡單地逃脫開始£與反引號中的列名:

INSERT INTO atm_data (ID, ... `£10`, `£20`, `£50`, `£100` ... 

測試案例:

CREATE TABLE tb (`£10` int); 
Query OK, 0 rows affected (0.05 sec) 

INSERT INTO tb (£10) VALUES (10); 
ERROR 1064 (42000): 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 '?10) VALUES (10)' at line 1 

INSERT INTO tb (`£10`) VALUES (10); 
Query OK, 1 row affected (0.00 sec) 
+0

感謝它的工作。 – user440798 2010-09-06 16:47:45