2010-09-06 92 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