Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#=========login================
def loginCheck(self):
    Username = self.txt_name.text()
    Password = self.txt_Password.text()

    mydb = mysql.connector.connect( host="localhost",  user="root",  passwd="",  database ="mydatabase")
    mydb = mydb.cursor()
    mydb.execute("select * from tbl_name where name ='"Username"' && password = '"Password"'")

    if(len(myresult = mydb.fetchone()) > 0):
        print("user found !")
    else:
        print("User Not found !")


What I have tried:

<pre>  File "d:\from\fromlogin.py", line 20
    mydb.execute("select * from tbl_name where name = '"Username"' && password = '"Password"';")
                                                               ^
SyntaxError: invalid syntax

the message it
i try but it still so error like this
Posted
Updated 11-Jun-19 0:22am

You need to use the + sign to concatenate literal strings with variable values:
Python
mydb.execute("select * from tbl_name where name = '" + Username + "' && password = '" + Password + "';")

However, you should never store passwords in clear text, it leaves your system vulnerable to hackers. Use a hash method to convert the password into a salted hash (Google it) for a more secure system.
 
Share this answer
 
In addition to what Richard has said, Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And with your current code, I can bypass yo password checking completely by adding "'--" to the end of any username ...

See here for some info on how to hash passwords: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and stored them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
Comments
Richard MacCutchan 11-Jun-19 6:42am    
I would have mentioned that, but I don't know how to do SQL with parameters in Python. My list of things to learn is getting longer rather than shorter. :(
OriginalGriff 11-Jun-19 6:45am    
Google knows:
https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/
Richard MacCutchan 11-Jun-19 6:56am    
Also at W3Schools, which is one of my gotos.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900