为了账号安全,请及时绑定邮箱和手机立即绑定

pymysql 拼接多条sql语句报错怎么办?

pymysql 拼接多条sql语句报错怎么办?

MM们 2019-02-24 02:40:36
sql语句是保存在sql.sql文件中的全是INSERT语句, 未遗漏 " ; " ** 如下代码可执行 ** with open('sql/sql.sql', "r", encoding="utf-8") as f: for each_line in f.readlines(): if not each_line or each_line == "\n": continue cur.execute(each_line.strip()) conn.commit() 但拼接sql后就报错 with open('sql/sql.sql', "r", encoding="utf-8") as f: for each_line in f.readlines(): if not each_line or each_line == "\n": continue elif count < 50: sql += each_line.strip() count += 1 else: cur.execute(sql) conn.commit() sql = each_line count = 1 if sql: cur.execute(sql) conn.commit() 错误信息 Traceback (most recent call last): File "main.py", line 135, in <module> db.readSQL() File "db.py", line 118, in readSQL cur.execute(sql) File "d:\Anaconda3\App\lib\site-packages\pymysql\cursors.py", line 165, in execute result = self._query(query) File "d:\Anaconda3\App\lib\site-packages\pymysql\cursors.py", line 321, in _query conn.query(q) File "d:\Anaconda3\App\lib\site-packages\pymysql\connections.py", line 860, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "d:\Anaconda3\App\lib\site-packages\pymysql\connections.py", line 1061, in _read_query_result result.read() File "d:\Anaconda3\App\lib\site-packages\pymysql\connections.py", line 1349, in read first_packet = self.connection._read_packet() File "d:\Anaconda3\App\lib\site-packages\pymysql\connections.py", line 1018, in _read_packet packet.check_error() File "d:\Anaconda3\App\lib\site-packages\pymysql\connections.py", line 384, in check_error err.raise_mysql_exception(self._data) File "d:\Anaconda3\App\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.ProgrammingError: (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 'INSERT INTO abc (字段名省略(显示不全),' at line 1") 请问这该怎么办? 还有, 数据库报错 Got a packet bigger than 'max_allowed_packet' bytes my.ini中max_allowed_packet=500M 重启数据库就好....
查看完整描述

4 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

sql语句有问题,你直接进入mysql shell执行一下试试

查看完整回答
反对 回复 2019-03-01
?
波斯汪

TA贡献1811条经验 获得超4个赞

pymysql.err.ProgrammingError: (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 'INSERT INTO abc (字段名省略(显示不全),' at line 1") 明显的有个语法错误,检查你的sql.sql文件

查看完整回答
反对 回复 2019-03-01
?
阿晨1998

TA贡献2037条经验 获得超6个赞

把你要执行的sql语句直接弄到mysql里去执行试试啊
一试就知道

查看完整回答
反对 回复 2019-03-01
  • 4 回答
  • 0 关注
  • 3031 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信