python - “str”对象没有属性“光标”
问题描述
connection_1 = MySQLdb.connect(host ="localhost:3306",user = " ",password = " ",db ="hospital")
def oracle1_database():
connection_1 = Connection_entry.get()
user = Username_entry.get()
passw = Password_entry.get()
db = connection_1
print(db)
cursor = db.cursor()
print("xz1",cursor)
a1 = cursor.execute("select * from a1")
print(a1)
try:
cursor.execute(a1)
myresult = cursor.fetchall()
for x in myresult:
savequery_data = x
print(savequery_data)
print("Query executed successfully")
except:
db.rollback()
print("Error occured")
请告诉我如何解决错误。
我创建了一个 GUI,其中输入 3 个输入 1 是连接字符串,2 个名称,3 是在 GUI 的帮助下输入连接时的密码
string(MySQLdb.connect(host ="localhost:3306", user = " ", password = " ", DB =" hospital"))
它提供了我在程序中编写的数据库查询的详细信息我有多个数据库(Oracle、MongoDB、SQL ......)当我在第一个输入框中写入连接字符串时,它需要我想要的数据库并运行查询。我在那里写代码,但我得到一个错误
Tkinter 回调 Traceback 中的异常
(最近一次调用最后一次):
文件“D:\python\envs\hospital data\lib\tkinter_init _.py ”,第 1883 行,调用
中 返回 self.func(*args)文件“D:/hospital data/mis1.py”,第 32 行,在 oracle1_database
cursor = a21.cursor()
AttributeError: 'str' object has no attribute 'cursor'
解决方案
错误告诉你究竟出了什么问题。您正在尝试调用.cursor()
字符串,而字符串没有光标。
这设置connection_1
为一个字符串:
connection_1 = Connection_entry.get()
这设置db
为connection_1
which 仍然是一个字符串:
db = connection_1
这试图调用.cursor()
on db
,它仍然是一个字符串:
cursor = db.cursor()
问题似乎是这些行中的第一行。您正确初始化connection_1
,但随后将其重置为字符串。不要那样做。
推荐阅读
- javascript - Json_encode 错误:未捕获的 SyntaxError:意外的令牌 ';' . 不知道如何解决
- c# - 包含存储过程的异步方法
- java - bean 创建回退的最佳实践是什么?
- ios - 我如何解释这个堆栈跟踪
- unit-testing - 由于 .tns,浏览器中的 NativeScript 单元测试失败。文件
- kubernetes - 无法在 nginx-ingress 上添加具有相同主机的多个 Ingress
- security - 云上的 Db2(仓库):如何使用 APIKEY 或 ACCESSTOKEN 从 CLP 连接?
- electron - 电子对话框在应用名称中显示“电子”而不是实际的应用名称
- python - 如果与前一个元素的差异小于值,则删除列表中的元素
- r - 试图理解 'dimnames' [2] 的错误长度不等于 R data.frame 中的数组范围