What if pymysql splices multiple sql statements and reports an error?

sql statements are
all INSERT statements saved in the sql.sql file, not omitted ";"

* *

< H2 > the following code can be executed < / H2 >

* *

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()

but an error will be reported after splicing 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()

error message

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")

what should I do about this?

also, the database reports an error

Got a packet bigger than "max_allowed_packet" bytes

max_allowed_packet=500M

in

my.ini

just restart the database.

Mar.11,2021
There is a problem with the

sql statement. Go directly to mysql shell and try


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 (field name is omitted (incomplete),' at line 1 ") there is an obvious syntax error. Check your sql.sql file


get the sql statement you want to execute directly into mysql to try.
you'll know


as soon as you try.

I don't know if you finally solved it

I have the same problem. I use spider under anaconda to write code, pymysql inserts multiple statements is no problem, but today, change the computer, use sublime text to run and report syntax error,sql statement absolutely no problem, print out into mysql direct execution is also ok. So I don't know what's wrong, why don't you try it in a different environment.

when max_allowed_packet passes parameters to connect, just add "max_allowed_packet=1000M" and so on.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ec09-2c450.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ec09-2c450.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?