我将JSON数据插入MySQL数据库
我正在解析JSON,然后使用python连接器将其插入MySQL数据库
通过试用,我可以看到错误与这段代码有关
for steps in result['routes'][0]['legs'][0]['steps']:
query = ('SELECT leg_no FROM leg_data WHERE travel_mode = %s AND Orig_lat = %s AND Orig_lng = %s AND Dest_lat = %s AND Dest_lng = %s AND time_stamp = %s')
if steps['travel_mode'] == "pub_tran":
travel_mode = steps['travel_mode']
Orig_lat = steps['var_1']['dep']['lat']
Orig_lng = steps['var_1']['dep']['lng']
Dest_lat = steps['var_1']['arr']['lat']
Dest_lng = steps['var_1']['arr']['lng']
time_stamp = leg['_sent_time_stamp']
if steps['travel_mode'] =="a_pied":
query = ('SELECT leg_no FROM leg_data WHERE travel_mode = %s AND Orig_lat = %s AND Orig_lng = %s AND Dest_lat = %s AND Dest_lng = %s AND time_stamp = %s')
travel_mode = steps['travel_mode']
Orig_lat = steps['var_2']['lat']
Orig_lng = steps['var_2']['lng']
Dest_lat = steps['var_2']['lat']
Dest_lng = steps['var_2']['lng']
time_stamp = leg['_sent_time_stamp']
cursor.execute(query,(travel_mode, Orig_lat, Orig_lng, Dest_lat, Dest_lng, time_stamp))
leg_no = cursor.fetchone()[0]
print(leg_no)
我已插入更高级别的详细信息,现在正在搜索数据库,以将此较低级别信息与其父级相关联。找到此唯一值的唯一方法是使用time_stamp搜索原点和目标坐标。我相信逻辑是合理的,并且在本节之后立即打印leg_no,我可以看到在第一次检查时出现的值是正确的
但是,当添加到其余代码时,它会导致后续部分使用游标插入更多数据而失败并出现此错误 -
raise errors.InternalError("Unread result found.")
mysql.connector.errors.InternalError: Unread result found.
此问题与MySQL Unread Result with Python
类似查询是否过于复杂且需要拆分还是存在其他问题?
如果查询确实太复杂,有人可以建议如何最好地拆分它吗?
编辑根据@Gord的帮助,我试图转储任何未读的结果
cursor.execute(query,(leg_travel_mode, leg_Orig_lat, leg_Orig_lng, leg_Dest_lat, leg_Dest_lng))
leg_no = cursor.fetchone()[0]
try:
cursor.fetchall()
except mysql.connector.errors.InterfaceError as ie:
if ie.msg == 'No result set to fetch from.':
pass
else:
raise
cursor.execute(query,(leg_travel_mode, leg_Orig_lat, leg_Orig_lng, leg_Dest_lat, leg_Dest_lng, time_stamp))
但是,我仍然得到
raise errors.InternalError("Unread result found.")
mysql.connector.errors.InternalError: Unread result found.
[Finished in 3.3s with exit code 1]
划伤头
编辑2 - 当我打印ie.msg时,我得到 -
No result set to fetch from
答案 0 :(得分:95)
所需要的只是将缓冲设置为true!
cursor = cnx.cursor(buffered=True)
答案 1 :(得分:16)
我能够重新创建你的问题。 MySQL Connector / Python显然不喜欢它,如果您检索多行并且在关闭游标或使用它来检索其他内容之前不会全部获取它们。例如
import mysql.connector
cnxn = mysql.connector.connect(
host='127.0.0.1',
user='root',
password='whatever',
database='mydb')
crsr = cnxn.cursor()
crsr.execute("DROP TABLE IF EXISTS pytest")
crsr.execute("""
CREATE TABLE pytest (
id INT(11) NOT NULL AUTO_INCREMENT,
firstname VARCHAR(20),
PRIMARY KEY (id)
)
""")
crsr.execute("INSERT INTO pytest (firstname) VALUES ('Gord')")
crsr.execute("INSERT INTO pytest (firstname) VALUES ('Anne')")
cnxn.commit()
crsr.execute("SELECT firstname FROM pytest")
fname = crsr.fetchone()[0]
print(fname)
crsr.execute("SELECT firstname FROM pytest") # InternalError: Unread result found.
如果您只想要(或关心)一行,那么您可以在查询中添加LIMIT
crsr.execute("SELECT firstname FROM pytest LIMIT 0, 1")
fname = crsr.fetchone()[0]
print(fname)
crsr.execute("SELECT firstname FROM pytest") # OK now
或者您可以使用fetchall()
在处理完所检索的行后删除任何未读的结果。
crsr.execute("SELECT firstname FROM pytest")
fname = crsr.fetchone()[0]
print(fname)
try:
crsr.fetchall() # fetch (and discard) remaining rows
except mysql.connector.errors.InterfaceError as ie:
if ie.msg == 'No result set to fetch from.':
# no problem, we were just at the end of the result set
pass
else:
raise
crsr.execute("SELECT firstname FROM pytest") # OK now
答案 2 :(得分:4)
cursor.reset()确实是您想要的。...
fetch_all()不好,因为您可能最终会将不必要的数据从数据库移动到客户端
答案 3 :(得分:0)
您与MySQL Workbench的连接也可能断开。再次建立连接。这为我解决了问题。
答案 4 :(得分:0)
是否将光标设置在for循环中,执行它,然后在循环帮助中再次将其关闭? 喜欢:
for steps in result['routes'][0]['legs'][0]['steps']:
cursor = cnx.cursor()
....
leg_no = cursor.fetchone()[0]
cursor.close()
print(leg_no)