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

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

重启数据库就好....

MM们
浏览 3032回答 4
4回答

慕村225694

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

波斯汪

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文件

阿晨1998

把你要执行的sql语句直接弄到mysql里去执行试试啊一试就知道
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python