如何将编码与数据库连接

时间:2018-07-12 01:35:27

标签: python database connect

嗨,我现在在raspberry pi 3中使用raspbian Stretch os。我不知道如何解决该错误,我认为我的编码无法与数据库连接

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()

这是我从cmd得到的错误

 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'

1 个答案:

答案 0 :(得分:0)

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