pymysql 提交sql语句报错问题

时间:2022-09-29 19:15:34

使用数据库插入时老是报错1054,而且还不是网上搜到的错误原因,经过测试多次才搞定,错误原因及改正如下:

原代码:

with conn:
cursor = conn.cursor()
for i in range(testnow.shape[0]):
dx, dy, oc, ot, x, y, house_id = testnow.iloc[i:i+1].values[0]
print ( house_id, dx, dy, oc, ot, x, y)
cursor.execute('insert into obj_infos (house_id, dx, dy, oc, ot, x, y) values ({}, {}, {}, {}, {}, {}, {})'.format(house_id,
dx, dy,
oc, ot,
x, y))
cursor.close()

报错:

E:\Anaconda\lib\site-packages\pymysql\connections.py in check_error(self)
391 errno = self.read_uint16()
392 if DEBUG: print("errno =", errno)
--> 393 err.raise_mysql_exception(self._data)
394
395 def dump(self):

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

InternalError: (1054, "Unknown column '客厅' in 'field list'")

更改后:


with conn:
cursor = conn.cursor()
for i in range(testnow.shape[0]):
dx, dy, oc, ot, x, y, house_id = testnow.iloc[i:i+1].values[0]
print ( house_id, dx, dy, oc, ot, x, y)
cursor.execute('insert into obj_infos (house_id, dx, dy, oc, ot, x, y) values ("{}", "{}", "{}", "{}", "{}", "{}", "{}")'.format(house_id,
dx, dy,
oc, ot,
x, y))
cursor.close()