如何修复“AttributeError:“模块”对象没有属性“连接”与Python mysql连接器?

qjp7pelc  于 2023-05-05  发布在  Mysql
关注(0)|答案(1)|浏览(107)

如何解决我的代码无法连接到MySQL数据库的问题?

import mysql, time

def login():
    db = mysql.connect(host='localhost',user='pi',password='1234',db='mydb')
    cursor = db.cursor()

    while True:
        usercode = input("Please enter your usercode: ")
        password = input("Please enter your password: ")

        sql = "SELECT * FROM `data` WHERE usercode = %s AND password = %s"
        curosr.execute(sql,(usercode,password))
        results = cursor.fetchall()

        if results:
                print("SUCCESS")
                break

        else:
            print("Usercode and password not recognised")
            again = input("Please press any number if you want to try again or * if you want to exit")
            if again.lower() == "n":
                print("Goodbye")
                time.sleep(1)
                break
login()

这是我在命令行上得到的错误:

pi@raspberrypi:~ $ python T1.py
Traceback (most recent call last):
  File "T1.py", line 26, in <module>
    login()
  File "T1.py", line 4, in login
    db = mysql.connect(host='localhost',user='pi',password='1234',db='mydb')
AttributeError: 'module' object has no attribute 'connect'
c90pui9n

c90pui9n1#

import mysql.connector
conn = mysql.connector.connect(host='localhost',user='pi',password='1234',db='mydb')

相关问题