赞
踩
- (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
- 'Darabont,弗兰克·德拉邦特,http://img3.doubanio.com/view/celebrity/s_ratio' at line 1")
SQL语句语法有问题了~
1、有一个方法能帮助我们更快的发现问题,就是输出自己的SQL语句:
输出Python中的MySQL插入语句:
- insert_sql = "INSERT INTO db_writers (writer_id, writer_name_en, writer_name, writer_avatars, writer_alt) VALUES ({},{},{},{},{});".format(writer_id, writer_name_en, writer_name, writer_avatars, writer_alt)
- print(insert_sql)
运行输出的SQL结果:
- INSERT INTO db_writers ( writer_id, writer_name_en, writer_name, writer_avatars, writer_alt )
- VALUES
- (
- 1047973,
- Frank Darabont,弗兰克·德拉邦特,
- http :// img3.doubanio.com / VIEW / celebrity / s_ratio_celebrity / public / p230.jpg,
- https :// movie.douban.com / celebrity / 1047973 /);
除了writer_id是int类型,其他字段都是varchar类型的,一看问题就明了了,varchar类型的是要用双引号括起来的,是因为我的SQL语句在使用{}的时候,在除了writer_id对应的{}外忘记用 ' ' 包起来了,本来应该用'{}'的。
"INSERT INTO db_writers (writer_id, writer_name_en, writer_name, writer_avatars, writer_alt) VALUES ('{}','{}','{}','{}','{}');".format(writer_id, writer_name_en, writer_name, writer_avatars, writer_alt)
这样便不会报错了
同时有时候在字符串内容中有 ' 比如:' Alia ' s '也会导致报错,这时候如果还要插入到数据库的话需要转义:
比如:
original_title = res['original_title'].replace("\'", "\\\'")
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。