首页 > 解决方案 > “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'

在此处输入图像描述

标签: pythonmysqlsqlmysql-workbenchmysql-python

解决方案


错误告诉你究竟出了什么问题。您正在尝试调用.cursor()字符串,而字符串没有光标。

这设置connection_1为一个字符串:

connection_1 = Connection_entry.get()

这设置dbconnection_1which 仍然是一个字符串:

db = connection_1

这试图调用.cursor()on db,它仍然是一个字符串:

cursor = db.cursor()

问题似乎是这些行中的第一行。您正确初始化connection_1,但随后将其重置为字符串。不要那样做。


推荐阅读