简体   繁体   English

使用Python执行INSERT INTO MySQL数据库时出现SQL错误

[英]SQL error when using Python to perform INSERT INTO a MySQL database

Receiving error when running Python code to perform 'INSERT INTO' 运行Python代码执行'INSERT INTO'时收到错误

ERROR: 错误:

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 '(1, 0.0, 0, 0, 6, 361, 28, 27, 0, 1, 4, 0.8733923470447209, 1);(0, 0.0, 0, 1, 6,' at line 1" 1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法'(1,0.0,0,6,361,28,27,0,1 ,4,0.8733923470447209,1);(0,0.0,0,1,6,'在第1行'

ProgrammingError Traceback (most recent call last) in ----> 1 cursor.execute(insert_query) 在----> 1 cursor.execute(insert_query)中编程错误跟踪(最近一次调用最后一次)

~\Anaconda3\lib\site-packages\pymysql\cursors.py in execute(self, query, args)
    168         query = self.mogrify(query, args)
    169 
--> 170         result = self._query(query)
    171         self._executed = query
    172         return result

~\Anaconda3\lib\site-packages\pymysql\cursors.py in _query(self, q)
    326         self._last_executed = q
    327         self._clear_result()
--> 328         conn.query(q)
    329         self._do_get_result()
    330         return self.rowcount

~\Anaconda3\lib\site-packages\pymysql\connections.py in query(self, sql, unbuffered)
    515                 sql = sql.encode(self.encoding, 'surrogateescape')
    516         self._execute_command(COMMAND.COM_QUERY, sql)
--> 517         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    518         return self._affected_rows
    519 

~\Anaconda3\lib\site-packages\pymysql\connections.py in _read_query_result(self, unbuffered)
    730         else:
    731             result = MySQLResult(self)
--> 732             result.read()
    733         self._result = result
    734         if result.server_status is not None:

~\Anaconda3\lib\site-packages\pymysql\connections.py in read(self)
   1073     def read(self):
   1074         try:
-> 1075             first_packet = self.connection._read_packet()
   1076 
   1077             if first_packet.is_ok_packet():

~\Anaconda3\lib\site-packages\pymysql\connections.py in _read_packet(self, packet_type)
    682 
    683         packet = packet_type(buff, self.encoding)
--> 684         packet.check_error()
    685         return packet
    686 

~\Anaconda3\lib\site-packages\pymysql\protocol.py in check_error(self)
    218             errno = self.read_uint16()
    219             if DEBUG: print("errno =", errno)
--> 220             err.raise_mysql_exception(self._data)
    221 
    222     def dump(self):

~\Anaconda3\lib\site-packages\pymysql\err.py in raise_mysql_exception(data)
    107         errval = data[3:].decode('utf-8', 'replace')
    108     errorclass = error_map.get(errno, InternalError)
--> 109     raise errorclass(errno, errval)

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 '(1, 0.0, 0, 0, 6, 361, 28, 27, 0, 1, 4, 0.8733923470447209, 1);(0, 0.0, 0, 1, 6,' at line 1")


for i in range(df_new_obs.shape[0]):
    insert_query += '('

    for j in range(df_new_obs.shape[1]):
        insert_query += str(df_new_obs[df_new_obs.columns.values[j]][i]) + ', '

    insert_query = insert_query[:-2] + ');'


---------------------------------------------------------------------

'INSERT INTO predicted_outputs VALUES (0, 0.0, 0, 1, 6, 179, 30, 19, 1, 0, 0, 0.1227992624349432, 0);(1, 0.0, 0, 0, 6, 361, 28, 27, 0, 1, 4, 0.8733923470447209, 1);(0, 0.0, 0, 1, 6, 155, 34, 25, 0, 2, 0, 0.26830495460343223, 0);(0, 0.0, 0, 1, 6, 179, 40, 22, 1, 2, 0, 0.1963853143922252, 0);(1, 0.0, 0, 0, 6, 155, 34, 25, 0, 2, 0, 0.7235017225544896, 1);(1, 0.0, 0, 0, 6, 225, 28, 24, 0, 1, 2, 0.7168905020985188, 1);(1, 0.0, 0, 0, 6, 118, 46, 25, 0, 2, 0, 0.5705238370679658, 1);(0, 0.0, 0, 1, 6, 179, 30, 19, 1, 0, 0, 0.1227992624349432, 0);(0, 0.0, 0, 1, 6, 118, 37, 28, 0, 0, 0, 0.13411805408968352, 0);(1, 0.0, 0, 0, 6, 118, 37, 28, 0, 0, 0, 0.5250065611752316, 1);(0, 0.0, 0, 1, 6, 378, 36, 21, 0, 2, 4, 0.45499802072862455, 0);(0, 0.0, 1, 0, 6, 118, 50, 31, 0, 1, 0, 0.6447416367376194, 1);(0, 0.0, 1, 0, 6, 233, 31, 21, 1, 1, 8, 0.35110766120784753, 0);(0, 0.0, 0, 1, 6, 179, 30, 19, 1, 0, 0, 0.1227992624349432, 0);(0, 0.0, 0, 0, 6, 235, 48, 33, 0, 1, 5, 0.09653346354354148, 0);(0, 0.0, 0, 0, 6, 268, 33, 25, 1, 0, 0, 0.1894785903905003, 0);(0, 0.0, 1, 0, 6, 118, 50, 31, 0, 1, 0, 0.6447416367376194, 1);(1, 0.0, 0, 0, 6, 179, 30, 19, 1, 0, 0, 0.49973827165859186, 0);(0, 0.0, 0, 1, 6, 291, 40, 25, 0, 1, 1, 0.38953149469289, 0);(1, 0.0, 0, 0, 7, 179, 30, 19, 1, 0, 0, 0.5001612226284466, 1);(0, 0.0, 0, 1, 7, 118, 37, 28, 0, 0, 0, 0.13431464561079212, 0);(0, 0.0, 0, 1, 7, 233, 31, 21, 1, 1, 8, 0.051366563789338106, 0);(1, 0.0, 0, 0, 7, 118, 37, 28, 0, 0, 0, 0.525428436293439, 1);(1, 0.0, 0, 0, 7, 118, 37, 28, 0, 0, 0, 0.525428436293439, 1);(0, 0.0, 0, 1, 7, 233, 31, 21, 1, 1, 8, 0.051366563789338106, 0);(0, 0.0, 0, 1, 7, 235, 43, 38, 0, 1, 0, 0.5123518738925141, 1);(0, 0.0, 1, 0, 7, 233, 31, 21, 1, 1, 8, 0.35149320375017246, 0);(1, 0.0, 0, 0, 7, 228, 58, 22, 0, 2, 1, 0.6333849402703989, 1);(0, 0.0, 0, 1, 7, 118, 37, 28, 0, 0, 0, 0.13431464561079212, 0);(1, 0.0, 0, 0, 7, 228, 58, 22, 0, 2, 1, 0.6333849402703989, 1);(0, 0.0, 0, 1, 7, 189, 33, 25, 0, 2, 2, 0.24494273576137351, 0);(0, 0.0, 0, 1, 7, 118, 37, 28, 0, 0, 0, 0.13431464561079212, 0);(0, 0.0, 0, 1, 7, 361, 28, 27, 0, 1, 4, 0.4919621401501115, 0);(0, 0.0, 0, 1, 7, 225, 28, 24, 0, 1, 2, 0.26224032774538253, 0);(1, 0.0, 0, 0, 7, 369, 31, 25, 0, 3, 0, 0.966560156232829, 1);(1, 0.0, 0, 0, 7, 289, 33, 30, 0, 2, 1, 0.9123989237228621, 1);(1, 0.0, 0, 0, 7, 235, 37, 29, 1, 1, 1, 0.7465975923060953, 1);(0, 0.0, 0, 0, 7, 118, 40, 34, 0, 1, 8, 0.023089501687401086, 0);(0, 0.0, 0, 0, 7, 231, 39, 35, 0, 2, 2, 0.29564748904572213, 0);(0, 0.0, 0, 0, 7, 179, 53, 25, 0, 1, 1, 0.07644956838010682, 0);'

You need commas between the bracketed rows, not semicolons. 你需要在括号内的行之间使用逗号,而不是分号。 Eg 例如

INSERT INTO MyTable VALUES (Value1, Value2), (Value1, Value2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM