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

标签 python mysql

运行 Python 代码执行“INSERT INTO”时收到错误

错误:

1064, "您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在 '(1, 0.0, 0, 0, 6, 361, 28, 27, 0, 1, 4, 0.8733923470447209, 1);(0, 0.0, 0, 1, 6,' 在第 1 行"

编程错误回溯(最近一次调用最后一次) 在 ----> 1 光标.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);'
<小时/>

最佳答案

括号内的行之间需要逗号,而不是分号。例如

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

关于python - 使用Python执行INSERT INTO MySQL数据库时出现SQL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55784197/

相关文章:

python - 如何调试占位符的输入?

python - 如何发起SIP( session 发起协议(protocol))并通过RTP(实时传输协议(protocol))传输媒体(语音)

python - 在 Python 中解码 Microsoft Safelink URL

php - 如何在 php 中使用 mysql_query 加载 sql 转储

两个表之间的 MySQL 条件

sql - MySQL 浮点值在插入时跳跃?

python - 在 Jupyter 实验室/控制台中通过 Pylint/pyreverse 创建 UML 图表

python - @njit 在组合数组方面的工作问题

php - 用额外的字段保存 HABTM?

php - 你如何在 php 中显示页面加载时间?