Mariadb python:命令不同步

问题描述

所以我试图为自己创建一个密码管理器,使用 python 和 mariadb。创建一个名为 pw 的表,其中包含 Name、Account 和 Passwords 3 列后,我尝试创建一个函数(Search_Passwords(app_name)),我可以用它来输入要在数据库搜索的关键字,它会给我正确的密码。但是,我遇到了此错误消息: Commands out of syncs error message.

我是 python 和 mariadb 的新手(由于某种原因使用它,MysqL 不起作用..),试图寻找一些答案,但仍然无法弄清楚。有人可以帮忙吗?以下是我认为可能相关的其他代码

This is what mariadb's table looks like.

Search_Passwords()

class UseDataBase

This is what I found online as a reference version where Search_Passwords() involved.

对不起,如果我的代码不完美... :(

解决方法

MariaDB Connector/Python 默认使用无缓冲结果集,这意味着在执行另一个游标之前,需要获取所有挂起的结果集或需要关闭游标。

例如下面的脚本

import mariadb

conn= mariadb.connect()
cursor1= conn.cursor()
cursor1.execute("select 1 from dual")
cursor2= conn.cursor()
cursor2.execute("select 2 from dual")

将抛出异常 Mariadb.InterfaceError: Commands out of sync; you can't run this command now

为避免这种情况,您需要创建一个缓冲游标:

cursor1= conn.cursor(buffered=True)