我能够像这样复制你的问题:
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中的关键字.如果不将它用作表名,您的生活将会更轻松.
但是,如果你真的想,你可以使用它,但是你必须引用它:
cursor.execute("""INSERT INTO `index`(url) VALUES(%s)""", (url,))
PS:不需要打电话
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"