#python #mysql
#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»
ProgrammingError Трассировка (последний последний вызов) в —-> 1 cursor.execute(insert_query)
~Anaconda3libsite-packagespymysqlcursors.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
~Anaconda3libsite-packagespymysqlcursors.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
~Anaconda3libsite-packagespymysqlconnections.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
~Anaconda3libsite-packagespymysqlconnections.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:
~Anaconda3libsite-packagespymysqlconnections.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():
~Anaconda3libsite-packagespymysqlconnections.py in _read_packet(self, packet_type)
682
683 packet = packet_type(buff, self.encoding)
--> 684 packet.check_error()
685 return packet
686
~Anaconda3libsite-packagespymysqlprotocol.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):
~Anaconda3libsite-packagespymysqlerr.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);'
Ответ №1:
Вам нужны запятые между строками, заключенными в квадратные скобки, а не точки с запятой. Например
INSERT INTO MyTable VALUES (Value1, Value2), (Value1, Value2);