MySQL的类型没有错,但是MySQL说错了1064

MySQL's type isn't wrong,but MySQL tell the wrong 1064

非常感谢!我用 'renew' 代替 'update'。它成功了! @MrTux


谢谢你的帮助。

mysql error:1064,但我的类型没有错

python3+mysql+pymysql

mysql table:(你只需要看到'update') (如果我插入不包括更新,它将成功。)

mysql> desc House;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| ID       | int(11)      | NO   | PRI | NULL    | auto_increment |
| num      | varchar(30)  | NO   | UNI | NULL    |                |
| url      | varchar(150) | YES  |     | NULL    |                |
| name     | varchar(50)  | YES  |     | NULL    |                |
| maintain | varchar(30)  | YES  |     | NULL    |                |
| update   | varchar(30)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

代码:

            update = "a str"
            sql2 = "INSERT INTO House (num,update) VALUES ('{0}' , '{1}'  )".format(num,update)
            print(sql2)


            try:
                cursor.execute(sql2)
                print("sql2 success")
                connect.rollback()
                print("rollback success")
            except Exception as e:
                print("sql2 wrong:" + str(e))

错误:

INSERT INTO House (num,update) VALUES ('NJ2578781985216667648' , 'a str'  )
sql2 wrong:(1064, "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 'update) VALUES ('NJ2578781985216667648' , 'a str'  )' at line 1")

您收到语法错误,因为 update 是 MySQL 中的关键字。您需要使用不同的名称或使用反引号引用:

sql2 = "INSERT INTO House (num,`update`) VALUES ('{0}' , '{1}'  )".format(num,update)

PS:还请注意可能的 SQL 注入。