我可以这样复制你的问题:mysql> create table `index` (url varchar(50));
Query OK, 0 rows affected (0.05 sec)
mysql> insert into index(url) values ('http://www.google.com');
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 'index(url) values ('http://www.google.com')' at line 1
mysql> insert into `index`(url) values ('http://www.google.com');
Query OK, 1 row affected (0.00 sec)
index是MySQL中的关键字。如果不使用它作为表名,您的生活会更轻松。
但是,如果你真的愿意,你可以用它,但是你必须引用它:
^{pr2}$
附言:不用打电话url = mdb.escape_string("http://www.google.com")
当你打电话时,MySQLdb会自动为你做这件事cursor.execute("""INSERT INTO index(url) VALUES(%s)""", (url,))
事实上,由于cursor.execute为您调用mdb.escape_string,因此根据url的值,您自己执行此操作可能会导致不希望的值插入到数据库中:In [105]: MySQLdb.escape_string("That's all folks")
Out[105]: "That\\'s all folks"
In [106]: MySQLdb.escape_string(MySQLdb.escape_string("That's all folks"))
Out[106]: "That\\\\\\'s all folks"