def login():
loggedIn = False
userInput = input("please enter your username: ")
pwdInput = input("please enter your password: ")
searchUser = hashlib.md5(userInput.encode()).hexdigest()
searchPwd = hashlib.md5(pwdInput.encode()).hexdigest()
query = """SELECT * FROM Users
WHERE Username =?;"""
cursor.execute(query, searchUser)
record = cursor.fetchone()
if record:
print("found")
Sorry the indenting is bad but isn't in the actual thing
I have this code set up to take in inputs and then hash them, I want to search the table "Users" to find the "Username". But it keeps saying "Incorrect number of bindings supplied. The current statement uses 1 and there are 32 supplied.
I tried combining the query and variables into 1 line and it still didnt work
You need to provide a tuple (or other sequence) of query parameters, even if there's only one:
cursor.execute(query, (searchUser,))
By passing searchUser on its own, without wrapping it, you're telling the cursor to treat the individual characters of searchUser as separate query parameters.
Related
I am trying to retrieve a password hash that i stored in my database. The problem is how i handle when the query is null.
In the case that I search for a user that exists the first print will print something but the in the second print that is inside the if statement it will output None
I don't understand what is happening. It seems to me that the variable is loosing it's value
db_password = c.execute("SELECT hashed FROM contas WHERE username=?", [username])
print(db_password.fetchone())
if(db_password.fetchone() != None):
print(db_password.fetchone())
hashed, = db_password.fetchone()
# Verify if passwords match
if ((bcrypt.checkpw(password.encode('utf8'), hashed) == False)):
print("Wrong credentials")
else:
print("User logged in successfully")
else:
print(db_password.fetchone())
print("User doesn't exist")
Each time you call db_password.fetchone() it fetches the next row of results. But your query only returns one row.
The call in the if statement fetches that row. Then the call in the print() call tries to fetch the next row, but there isn't another row, so it prints None. Then the third call in the variable assignment tries to fetch the next row, but there still isn't another row, so you get an error because you're trying to assign None in a tuple assignment.
You should fetch into a variable. Then you can test that and use it in the assignment.
row = db_password.fetchone()
if row:
print(row)
hashed = row[0]
...
else:
print("user doesn't exist")
Calling fetchone() will move the cursor to the next row each time and return None if there are no rows available (see documentation here). If you want to just check the one password, store the result of the fetchone call in a variable and use it for future comparisons/printing, i.e.
password = db_password.fetchone()
print(password)
if password is not None:
print(password) # If password is not None, this will print the same thing as the previous print call
...
else:
...
Im trying to add an input string function like this,
r = requests.get('https://www.roblox.com/profile?userid=' + userid)
userid = input("Please enter a valid user ID: ")
print(r.text[0:100])
It doesn't work for me and I believe I am doing something wrong.
I want to make it so when the person types in a valid userid it adds it into the "requests.get" function and basically makes it apart of the url.
You need to declare userid before you add it to your string. The following code will work.
userid = input("Please enter a valid user ID: ")
r = requests.get('https://www.roblox.com/profile?userid=' + userid)
print(r.text)
You need to put the input before.
userid = input("Please enter a valid user ID: ")
r = requests.get('https://www.roblox.com/profile?userid=' + str(userid))
print(r.text[0:100])
Have you tried switching the order of the steps?
Ask for the input and store it as userid
Add that to the URL as part of the requests.get()
Python (and most programming languages) tend to evaluate steps in order, from the top down, so the order of the statements is often important.
userid = input("Please enter a valid user ID: ")
r = requests.get('https://www.roblox.com/profile?userid=' + userid)
print(r.text[0:100])
In your case, userid variable is not defined so you have a compilation-like error, just change the position of the statements like so
userId = input()
r = requests.get(f"url?id={userId}")
You need to change the order of your commands.
When the r = requests.get('https://www.roblox.com/profile?userid=' + userid) is executed, the request is already done, you can't edit it.
Try something like this:
userid = input("Please enter a valid user ID: ")
r = requests.get('https://www.roblox.com/profile?userid=' + userid)
print(r.text[0:100])
hi i am trying to make a simple login module in tkinter for that i am taking input from the user via the entry widget and comparing that password to the password in database via a if loop... but for some reason the password doesnt match, so i tried some tests and it dosent match ....
import sqlite3
conn=sqlite3.connect('auth_rWebsites.db')
c=conn.cursor()
def check_password(key):
thePass= c.execute("SELECT pass FROM user_pass")
if key == thePass:
print("right Password")
else:
print("Wrong Password")
rows= c.execute("SELECT * FROM user_pass")
for row in rows:
print row
check_password("root")
this the above simple test code i wrote..
output is
(u'root',)
Wrong Password
so someone please explain me why is this happening and what do i have to do to match it ....
thePass is an iterable that yeilds tuples, key is a string.
In general, the python db api always returns database rows as tuples because you can select multiple things in a statement. Rather than having confusing logic that needs to look at the query to determine the return type of the row (for both the database api and the caller), the spec says to just return a tuple always.
You need to compare key with the row correctly...
rows = c.execute("Select pass FROM user_pass")
for row in rows:
if row[0] == thePass:
print("right password")
return
else:
print("Wrong Password")
Note, this probably isn't what you actually want. All it does is verify that one row in the user_pass table has that pass value. More likely, you want to filter the query against the username and then check the password:
c.execute("SELECT pass FROM user_pass WHERE username = ?", username)
result = c.fetchone() # username should be unique I assume :-)
if result[0] == salt_and_hash(key):
print("Yeah!")
else:
print("No!")
c.execute("SELECT pass FROM user_pass") is returning a tuple, not a string, but key is a string. You need to get the first value from the tuple and compare that to your key argument, so change your function to this:
def check_password(key):
thePass= c.execute("SELECT pass FROM user_pass")[0] # Changed here
if key == thePass:
print("right Password")
else:
print("Wrong Password")
I'm writing this code to verify that a user-provided value and key match what I have in my dict. When they match, the Welcome portion of the code will run, but if one of them is wrong then it will have to run the else statement.
name = input("Name: ")
code = input("Code: ")
users = {'rafiki':'12345', 'wanjiru':'12334', 'madalitso':'taxpos'}
if _______ in users: #at this point is where i need your support, yoverify both key and its value at once.
print ("Welcome back %s " % username)
else:
print ("Please check if your name or Code are correctly spelled")
In a single operation:
if users.get(name) == code:
Because dict.get return None for an unknown key instead of throwing a KeyError
Use this:
if name in users and users[name] == code:
print "Welcome back %s" % username
This should work:
if name in users and code == users[name]:
I am writing a python function that takes an input from a user, searches for it in a database and then checks the input against the return result and if correct proceeds with the program and if incorrect asks the user to re-enter. This doesn't work however. When a user enters something it just says it is incorrect as per my program. Here is the code:
def TakeOutItem():
Serial = input("Please enter the serial number of the music you are taking out: ")
SerialN = int(Serial,)
with sqlite3.connect("school.db") as db:
cursor = db.cursor()
cursor.execute("SELECT SerialNo FROM Music WHERE SerialNo=?",(SerialN,))
data = cursor.fetchone()[0]
print(">>",SerialN, data,">>")
print(type(SerialN),type(data)).show
print(int(SerialN) == int(data))
if SerialN == int(data):
DateOut = date.today()
DateIn = date.today() + timedelta(days=30)
cursor.execute('SELECT Name FROM Music WHERE SerialNo=?', Serial)
NameofMusic = cursor.fetchone()
cursor.execute('SELECT StudentID FROM Student WHERE username=?', student_login(username))
StudentID = cursor.fetchone()
cursor.execute('INSERT INTO MusicLoan VALUES (?,?,?,?,?)', DateOut, DateIn, Music, NameofMusic, StudentID)
print("Item taken out successfully")
student_menu()
else:
print("Incorrect Serial Number please try again")
TakeOutItem()
and this is the error screen
What would you like to do?
1. Take out item
2. Return item
3. Exit
Please select an option number: 1
Please enter the serial number of the music you are taking out: 1
>> 1 1 >>
<class 'int'> <class 'int'>
Please select a valid option
The it takes me back to he previous menu
How can I fix this error??
cursor.fetchone() will always return a tuple.So use
data = cursor.fetchone()[0]
or
if SerialN == data[0]:
Inside your if condition also, please rewrite it to NameofMusic[0] and StudentID[0] in your INSERT query