簡體   English   中英

檢查用戶輸入的用戶名和密碼在MYSQL數據庫中是否存在

[英]Checking whether user entered username and password exists in MYSQL database

我是python的新手,我正在創建一個程序來檢查所需用戶輸入的用戶名和密碼是否存在於數據庫中。 這是代碼:

 import mysql.connector
 class data:
 def __init__(self):

    self.usernam = input("Enter user name: ")
    self.password= input("Enter Password: ")

 a=data()
 conn = mysql.connector.connect(host='localhost',user='root',passwd='root',database='user')


 loadname= ("select username from user where username='%s'")
 mycursor=conn.cursor()
 mycursor.execute(a.usernam, loadname)
 usercheck=mycursor.fetchone()
 loadpass= ("select pass from user where pass ='%s'")
 mycursor2=conncursor()
 mycursor2.execute(a.password,loadpass)
 passcheck=mycursor2.fetchone()


 if a.username == usercheck and a.password == passcheck:
     print ("pass")

 else:
     print ("sorry")
 conn.commit()
 conn.close()

我收到以下錯誤:

   Traceback (most recent call last):
     File "C:\Users\HP\Desktop\2nd.py", line 16, in <module>
     mycursor.execute(a.usernam, loadname)
     File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line                                 507, in execute
     self._handle_result(self._connection.cmd_query(stmt))
     File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 722, in cmd_query
     result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
     File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 640, in _handle_result
      raise errors.get_exception(packet)
     mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'asaa' at line 1
mycursor.execute( loadname,a.username)

首先查詢,然后是參數...

我強烈建議您立即切換到像sqlalchemy這樣的ORM

我不知道“ mysql.connector”,但如果它作為MySQLdb工作,則應該遵循以下原則:

mycursor=conn.cursor()
mycursor.execute("select username from user where username=%s", a.username)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM