1. 程式人生 > >mySql Translating SQLException with SQL state '42000', error code '1064'

mySql Translating SQLException with SQL state '42000', error code '1064'

今天建立表,在執行insert句語時,mySql 報錯了 Translating SQLException with SQL state '42000', error code '1064'

建表語法:

CREATE TABLE `app_sms_t` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `mobile` varchar(11) DEFAULT '' COMMENT '手機號',
  `smscode` varchar(10) DEFAULT '' COMMENT '驗證碼',
  `content` varchar(500) DEFAULT '' COMMENT '簡訊內容',
  `condition` varchar(30) DEFAULT '' COMMENT '使用場景',
  `status` smallint(6) DEFAULT '0' COMMENT '傳送狀態.0:未傳送,1:已傳送,2:成功,3:傳送失敗',
  `ip_address` varchar(40) DEFAULT NULL COMMENT '客戶端IP',
  `create_time` datetime DEFAULT NULL COMMENT '建立時間',
  `update_time` datetime DEFAULT NULL COMMENT '更新時間',
  `remark` varchar(200) DEFAULT '' COMMENT '備註',
  `disabled` smallint(6) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

insert語句略。

異常資訊如下:

Translating SQLException with SQL state '42000', error code '1064', 
message [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 'condition,
			status,
			ip_address,
			create_time,
			update_time,
			remark,
' at line 5]; SQL was [] for task [
### Error updating database.  Cause: 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 'condition,
			status,
			ip_address,
			create_time,
			update_time,
			remark,
' at line 5

報錯分析:
欄位 condition 附近有語法錯誤,但是我看了insert語法,沒發現問題。
將 欄位 condition 改成 conditions 後,又丟擲相同的異常:欄位 status 附近有語法錯誤。

原因:

建表的某些欄位中 ( condition , status ) 使用了MySql 保留字

保留字 分為兩種:

  • 顯示保留字。 如select , from , group by
  • 隱式保留字。 status , int 等

解決方法:

更改欄位的名稱,如把 condition 改成 conditionsstatus 改成 send_status

mysql 保留字 如下:

ADD	ALL	ALTER
ANALYZE	AND	AS
ASC	ASENSITIVE	BEFORE
BETWEEN	BIGINT	BINARY
BLOB	BOTH	BY
CALL	CASCADE	CASE
CHANGE	CHAR	CHARACTER
CHECK	COLLATE	COLUMN
CONDITION	CONNECTION	CONSTRAINT
CONTINUE	CONVERT	CREATE
CROSS	CURRENT_DATE	CURRENT_TIME
CURRENT_TIMESTAMP	CURRENT_USER	CURSOR
DATABASE	DATABASES	DAY_HOUR
DAY_MICROSECOND	DAY_MINUTE	DAY_SECOND
DEC	DECIMAL	DECLARE
DEFAULT	DELAYED	DELETE
DESC	DESCRIBE	DETERMINISTIC
DISTINCT	DISTINCTROW	DIV
DOUBLE	DROP	DUAL
EACH	ELSE	ELSEIF
ENCLOSED	ESCAPED	EXISTS
EXIT	EXPLAIN	FALSE
FETCH	FLOAT	FLOAT4
FLOAT8	FOR	FORCE
FOREIGN	FROM	FULLTEXT
GOTO	GRANT	GROUP
HAVING	HIGH_PRIORITY	HOUR_MICROSECOND
HOUR_MINUTE	HOUR_SECOND	IF
IGNORE	IN	INDEX
INFILE	INNER	INOUT
INSENSITIVE	INSERT	INT
INT1	INT2	INT3
INT4	INT8	INTEGER
INTERVAL	INTO	IS
ITERATE	JOIN	KEY
KEYS	KILL	LABEL
LEADING	LEAVE	LEFT
LIKE	LIMIT	LINEAR
LINES	LOAD	LOCALTIME
LOCALTIMESTAMP	LOCK	LONG
LONGBLOB	LONGTEXT	LOOP
LOW_PRIORITY	MATCH	MEDIUMBLOB
MEDIUMINT	MEDIUMTEXT	MIDDLEINT
MINUTE_MICROSECOND	MINUTE_SECOND	MOD
MODIFIES	NATURAL	NOT
NO_WRITE_TO_BINLOG	NULL	NUMERIC
ON	OPTIMIZE	OPTION
OPTIONALLY	OR	ORDER
OUT	OUTER	OUTFILE
PRECISION	PRIMARY	PROCEDURE
PURGE	RAID0	RANGE
READ	READS	REAL
REFERENCES	REGEXP	RELEASE
RENAME	REPEAT	REPLACE
REQUIRE	RESTRICT	RETURN
REVOKE	RIGHT	RLIKE
SCHEMA	SCHEMAS	SECOND_MICROSECOND
SELECT	SENSITIVE	SEPARATOR
SET	SHOW	SMALLINT
SPATIAL	SPECIFIC	SQL
SQLEXCEPTION	SQLSTATE	SQLWARNING
SQL_BIG_RESULT	SQL_CALC_FOUND_ROWS	SQL_SMALL_RESULT
SSL	STARTING	STRAIGHT_JOIN
TABLE	TERMINATED	THEN
TINYBLOB	TINYINT	TINYTEXT
TO	TRAILING	TRIGGER
TRUE	UNDO	UNION
UNIQUE	UNLOCK	UNSIGNED
UPDATE	USAGE	USE
USING	UTC_DATE	UTC_TIME
UTC_TIMESTAMP	VALUES	VARBINARY
VARCHAR	VARCHARACTER	VARYING
WHEN	WHERE	WHILE
WITH	WRITE	X509
XOR	YEAR_MONTH	ZEROFILL