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
Related
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.
I am building a budget program, and I am new to python,a month into it. I started this project in class to push myself. Well I haven't taken a database class yet and we only briefly touch on it. The issue I am having is when I am passing a tuple to sqlite3 to put the values into the corresponding fields. I have 2 files. The functions file, that is only for the functions not relating to direct database manipulation. The other file contains all the functions for database creation to manipulation.
Here is the code that is giving me problems
import budget_buddy
total_money = 0
def deposits():
"""Deposits function so we can call it later on"""
return float(input('Please enter deposit amount:'))
def input_expense_item() -> tuple:
"""
Prompt the user to enter an expense type, and the amount for the expense
:return: Tuple - expense type, amount
"""
expense_type = input("What is the expense item?\n").capitalize()
cost = None
# must ensure that the user provides a valid number! In this case, a valid decimal!
while not cost:
temp = input(f"How much was '{expense_type}'?\n")
try:
cost = float(temp)
except ValueError:
print(f"{temp} - was an invalid entry. Please make sure you entered a valid number")
# This is how to return a tuple. This means you can assign two variables when calling this function as seen below
return expense_type, cost
#def balance(expense_amount: int):
#"""Balance function to be used later on as well"""
#return total_money - expense_amount
def cash_on_hand():
"""
Cash_on_hand function, this is asking how much money not in bank
accounts do you have.
"""
return float(input('How much money do you have on hand:'))
while True:
menu_option = input("What option do you want to do?\n"
"1: Enter deposit\n"
"2: Enter cash on hand\n"
"3: Enter expenses\n"
"4: Monthly Deposit Total\n")
if menu_option == "1":
deposit_amount = deposits()
#total_money += deposit_amount
budget_buddy.insert_deposits(deposit_amount)
elif menu_option == "2":
cash_amount = cash_on_hand()
#total_money += cash_amount
budget_buddy.insert_cash(cash_amount)
elif menu_option == "3":
#new_expense_type, new_expense_amount = input_expense_item()
#print(f"The user wants to add {new_expense_type} for ${new_expense_amount}")
expense_type, cost = input_expense_item()
budget_buddy.insert_expenses(input_expense_item)
#Code below was used previously, attempting changes but keeping the code just in case for a fall back
#current_balance = balance(new_expense_amount) + cash_on_hand()
#total_money = current_balance
#print(f"Total balance is ${total_money}")
elif menu_option == "4":
budget_buddy.monthly_deposit_total()
Please ignore the comments, this code has been changed a lot since making the start of the program. And I am actively changing the way I do it.
The next is my database code,
# Importing OS module to determine desktop path
import os
import sqlite3
from datetime import datetime
from sqlite3 import Error
"""Current month with %B argument,which displays entire month name"""
current_month = datetime.now().strftime('%B')
"""Current time in year-month-day. Followed by Hour:Minute.
Used to track when data was entered.
Not used as of yet on 2020-10-17, but will be as the program develops."""
current_time = datetime.now().strftime('%Y-%m-%d')
current_time_iso = datetime.now().isoformat(sep=' ',timespec='minutes')
"""Using os module to determine the current users desktop path so we can
make the budget database file on the desktop so it is easier to find.
Sqlite3 module and Error module as well to create a connection to the
database after it is made, returning error (e) if the database does not exist."""
desktop = os.path.join(os.path.join(os.environ["USERPROFILE"]), "Desktop")
default_path = os.path.join(desktop, "budget.db")
if os.path.exists(default_path):
print('Database already exists\n')
pass
else:
print(f'Creating database budget.db at {desktop}')
pass
def sql_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
print('Connected succsefully to budget.db using Sqlite3 version {}!'.format(sqlite3.version))
return conn
except Error as e:
print(e)
finally:
if not conn:
conn.close()
def monthly_table(conn):
cursorObj = conn.cursor()
cursorObj.execute(f"CREATE TABLE IF NOT EXISTS {current_month} (Deposit float, Deposit_Date real, Expense_Amount float, Expense_Type text, Cash_on_Hand float, Balance float)")
conn.commit()
print(f'Table {current_month} successfully created in budget.db')
conn = sql_connection(default_path)
if conn:
monthly_table(conn)
else:
print('Exiting')
def insert_deposits(amt):
insert_command_amt = """insert into {} (Deposit, Deposit_Date) values (?,?)""".format(current_month)
insert_amount = amt
insert_timestamp = current_time_iso
multi_insert_date_amount = insert_amount, insert_timestamp
conn.execute(insert_command_amt, multi_insert_date_amount)
conn.execute("commit;")
def monthly_deposit_total():
# mdt = monthly deposit total
mdt_query = """Select (Deposit) From {}""".format(current_month)
cursor = conn.cursor()
cursor.execute(mdt_query)
total = 0
for row in cursor:
total += row[0]
print(f"Your total deposits this month is: ${total}")
def insert_expenses(text,amt):
insert_expense_command = """insert into {} (Expense_Type, Expense_Amount) values (?,?)""".format(current_month)
insert_expense_name = str()
insert_expense_amt = amt
multi_expense_insert = insert_expense_name, insert_expense_amt
conn.execute(insert_expense_command, multi_expense_insert)
conn.execute("commit;")
My issue is the last function here,
def insert_expenses(text,amt):
insert_expense_command = """insert into {} (Expense_Type, Expense_Amount) values (?,?)""".format(current_month)
insert_expense_name = str()
insert_expense_amt = amt
multi_expense_insert = insert_expense_name, insert_expense_amt
conn.execute(insert_expense_command, multi_expense_insert)
conn.execute("commit;")
which is used by the input_expense_item function in the first code block which is my functions file.
I have been pouring over the code to find my error causing the issue but I cannot find it.
I understand it may not make sense to others why I am doing seperate tables in the database but it is how it will be done, that is not changing because I have plans for it.
The exact error I am getting is this
File "F:\Documents\GitHub\Budget_buddy\functions_for_BB.py", line
80, in
budget_buddy.insert_expenses(input_expense_item)
TypeError: insert_expenses() missing 1 required positional argument:
'amt'**
The program works as intended up until it tries to push the values to the database for the expense command.
input_expense_items returns a tuple, which is one "object", therefore, nothing is being sent to amt when insert_expenses is called. The tuple represents the exact object needed for the placeholder argument to execute. If insert_expenses takes one argument, the parsing steps can be eliminated and the argument variable will be the second arg to execute.
Okay so I finally got it fixed thank to some help. Here is what I did to get it to work as intended.
import budget_buddy
def deposits():
"""Deposits function so we can call it later on"""
return float(input('Please enter deposit amount:'))
def input_expense_item() -> tuple:
"""
Prompt the user to enter an expense type, and the amount for the expense
:return: Tuple - expense type, amount
"""
expense_type = input("What is the expense item?\n").capitalize()
cost = None
# must ensure that the user provides a valid number! In this case, a valid decimal!
while not cost:
temp = input(f"How much was '{expense_type}'?\n")
try:
cost = float(temp)
except ValueError:
print(f"{temp} - was an invalid entry. Please make sure you entered a valid number\n")
print(f'User wants to add {expense_type} for ${cost}\n')
return expense_type, cost
def cash_on_hand():
"""
Cash_on_hand function, this is asking how much money not in bank
accounts do you have.
"""
return float(input('How much money do you have on hand:'))
while True:
menu_option = input("What option do you want to do?\n"
"1: Enter deposit\n"
"2: Enter Expenses\n"
"3: Monthly Deposits Total\n"
"4: Monthly Balance\n\n")
if menu_option == "1":
deposit_amount = deposits()
budget_buddy.insert_deposits(deposit_amount)
elif menu_option == "2":
budget_buddy.insert_expenses(*input_expense_item())
elif menu_option == "3":
budget_buddy.monthly_deposit_total()
elif menu_option == "4":
#this command will take the deposits, subtract expenses and ask user if they want to add cash on hand
print('Command not implimented yet, please try again later')
below is what changed in the functions file, I unpacked the tuple in place(?) while calling the function to be used and it passes the tuple values on over
elif menu_option == "2":
budget_buddy.insert_expenses(*input_expense_item())
As you can see below where it uses the values.
def insert_expenses(expense_type, cost):
insert_expense_command = """insert into {} (Expense_Type, Expense_Amount) values (?,?)""".format(current_month)
insert_expense_name = expense_type
insert_expense_amt = cost
multi_expense_insert = insert_expense_name, insert_expense_amt
conn.execute(insert_expense_command, multi_expense_insert)
conn.execute("commit;")
I'm doing a code where the program ask for personal informations of someone, then he reads those infomations and send to the database. So I have the option to register and to consult. I don't know if the program is with more problems because I need to fix it first.
When I try to register the person It's give me the error "Is not possible to register." and I can't find why.
import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()
def criardb():
c.execute('CREATE TABLE IF NOT EXISTS pessoas(id INTEGER PRIMARY KEY
AUTOINCREMENT,nome VARCHAR, idade INT, tel VARCHAR, pais VARCHAR)')
conn.commit()
def insertdata(nome,idade,tel,pais):
c.execute = ('INSERT INTO pessoas VALUES (?,?,?,?)',
(nome,idade,tel,pais))
conn.commit()
def consultdata():
sql = c.execute('SELECT * FROM pessoas')
for row in sql:
print("Nome: {}".format(row[0]))
def consultdataind(esc):
sql = c.execute('SELECT * FROM pessoas WHERE id = ?')
for row in sql(sql,(esc)):
print("Nome: {} Idade: {} Telefone: {} País:
{}".format(row[0],int(row[1]),row[2],row[3]))
try:
print("Creating database...")
criardb()
except:
print("ERRO: It was not possible to create the database.")
else:
print("Database successfully created.")
while True:
print("Welcome to the register system, choose a option:")
op = int(input("| 1 - Register | 2 - Consult | 3 - Exit | "))
if op == 1:
n = input("Nome: ")
i = int(input("Idade: "))
t = input("Telefone: ")
p = input("País: ")
try:
insertdata(n,i,t,p)
except:
print("ERRO: It's not possible to register")
else:
print("Successfully registered.")
elif op == 2:
print("List of registered users:")
try:
consultdata()
except:
print("It was not possible to load the list.")
print("Write the person ID to individual consult.")
esc = int(input(">> "))
consultdataind(esc)
elif op == 3:
break
else:
print("Invalid command.")
I need to send all the information to the database and return the consult, first it will show all the registered person then the user can write the respective ID of someone in the list and it will show all the details about this person
Replace your insertdata with this and everything should work fine.
def insertdata(nome,idade,tel,pais):
c.execute('INSERT INTO pessoas (nome,idade,tel,pais) VALUES (?,?,?,?)', [nome,idade,tel,pais])
conn.commit()
You need to call the execute method of the cursor here.
And on the side note, never use except directly without specifying an exception. It hides the simplest of error messages which will make your code very hard to debug. As of now it was AttributeError that was causing this problem as you were trying to assign value to Cursor which is ReadOnly
import sqlite3
conn = sqlite3.connect('LeVinEmployee.db')
profile = input("Are you a user? y/n: ")
if profile == 'y':
login = input("Enter login name: ")
#passw = input("Enter password: ")
c = conn.cursor()
c.execute("SELECT * FROM Employee WHERE Email = '" + login + "'")
result = c.fetchone()
if result[0] == 1:
print(c.fetchall())
else:
print("not")
else:
print("You are not a user")
What I am trying to do here is pretty simple. I am trying to create user login function. If user type 'y', program will simply ask to put login email. If user type correct email from database, print that customer information. if wrong, print 'not'. I am not sure what is wrong with my code. Can someone help me please?
As I understand, email is an unique field. And your query could return one record or nothing (None). Try
result = c.fetchone()
if result: # result could be None or tuple (record)
print(result)
else:
print("not")
Also, such method of parameter pass is incorrect (insecure)
c.execute("SELECT * FROM Employee WHERE Email = '" + login + "'")
use
c.execute("SELECT * FROM Employee WHERE Email=?", login)
more info here.
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")