'str'对象没有属性'cursor'

问题描述

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个是密码。

string(MysqLdb.connect(host ="localhost:3306",DB =" hospital")) 

它提供了我在程序中编写的数据库查询的详细信息,当我在第一个输入框中编写连接字符串时,它具有多个数据库(Oracle,MongoDB,sql ....),它将获取我想要的数据库并运行查询。我在那里写代码,但出现错误

Tkinter回调中的异常
追溯(最近一次通话):
调用
中的文件“ D:\ python \ envs \ hospital data \ lib \ tkinter_ init _。py”,第1883行 返回self.func(* args)

oracle1_database中的文件“ D:/ hospital data / mis1.py”,第32行,
cursor = a21.cursor()
AttributeError:“ str”对象没有属性“ cursor”

enter image description here

解决方法

错误告诉您确切的问题。您正在尝试在字符串上调用os_log,并且字符串没有光标。

这会将.cursor()设置为字符串:

connection_1

这会将connection_1 = Connection_entry.get() 设置为db,它仍然是字符串:

connection_1

这会尝试在db = connection_1 上调用.cursor(),它仍然是一个字符串:

db

问题似乎出在那几行中。您正确初始化了cursor = db.cursor() ,但随后将其重置为字符串。不要那样做。