如何将编码与数据库连接起来
how can I connect the coding with database
嗨,我现在在 raspberry pi 中使用 raspbian stretch os 3. 我不知道如何解决错误,我认为我的编码无法连接数据库
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'
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='pi',password='1234',db='mydb')
嗨,我现在在 raspberry pi 中使用 raspbian stretch os 3. 我不知道如何解决错误,我认为我的编码无法连接数据库
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'
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='pi',password='1234',db='mydb')