I am trying to update my user detail with Python and SQLite.
The aim is to upgrade all the columns of my user in one go.
My code is:
def update():
new_username = input("Input your NEW username:\n")
new_firstname = input("Input your NEW firstname:\n")
new_lastname = input("Input your NEW lastname:\n")
new_email = input("Input your NEW email:\n")
new_password = input("Input your NEW password:\n")
update_customer =("""UPDATE customer SET username = ? AND firstname = ? AND lastname = ? AND email = ? AND password = ?""")
cursor.execute(update_customer, [(new_username), (new_firstname), (new_lastname), (new_email), (new_password)])
I inspected the database before and after running my python function. However, the changes are not saved into the database. Nothing changes but the username that disappears.
You don't use AND for additional columns to be SET. Instead you separate the columns to be SET with a comma.
So you want
update_customer =("""UPDATE customer SET username = ?, firstname = ?, lastname = ?, email = ?, password = ?""")
and then a WHERE clause if not setting all rows to the same values.
As per :-
SQL As Understood By SQLite - UPDATE
You need to save changes after you completed the transaction.
cursor = conn.cursor() # Get cursor
cursor.execute(...) # Execute some SQL queries
# This is the line you've missed.
# You need to call this function every time you update the data in database.
cursor.commit()
Also, your SQL syntax for the "UPDATE" command is not correct. Use commas instead of "AND" when specifying multiple columns to be changed. Like this:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Related
I am trying to add a feature to my program where a teacher sets homework to users from a class they've made. There is a table for users where each user has a unique UserID, classname, firstname and surname. I am trying to take the userIDs of students who are in a certain class, and insert them into a HomeworkSet table. I am able to retrieve the userIDs successfully, but when I insert them into the HomeworkSet table, the values appear as (for example) ('2a1910e919a84230bfc2a7111160cade',), and I am not sure how I am meant to remove the brackets and apostraphes.
def Class_sethw():
homeworktoset = Homework_To_Set.get()
#print (homeworktoset)
conn = sqlite3.connect('MyComputerScience.db')
c = conn.cursor()
homeworkID = c.execute("SELECT HWID FROM HomeworkInfo WHERE HomeworkName = ?", (homeworktoset, )).fetchone()
print (homeworkID)
c.execute("SELECT UserID FROM users WHERE ClassName = ?", (ClassName_SetHWR, ))
homeworksetlist = c.fetchall()
print (homeworksetlist)
for i in (homeworksetlist):
#x = i
#firstname, lastname = x.split(" ")
c.execute('insert INTO HomeworkSet (HWID, StudentID)VALUES(?,?);', ((homeworkID[0]), str(i)))
conn.commit()
Label(sethw, text = "Homework Set!", fg = "GREEN").place(relx=0.205, rely=0.445, height=34, width=97)
This is the code I have used.
You should change this line:
for i in (homeworksetlist):
to:
for i in homeworksetlist:
I am trying to query whether the staff ID and password are correct. The query works, however I want to get the fname and lname data from the database and print certain fields. It seems as though the row[fname] and row[lname] doesn't work... is there another way to do this? I keeps saying fname is not defined.
import sqlite3
connection = sqlite3.connect('shop.db')
cursor = connection.cursor()
create_table = '''
CREATE TABLE IF NOT EXISTS employee (
staff_ID INTEGER PRIMARY KEY,
password CHAR(4),
fname VARCHAR(20),
lname VARCHAR(30));'''
cursor.execute(create_table)
staffid = input('staff id: ')
password = input('password: ')
cursor.execute('SELECT * FROM employee WHERE staff_ID=? AND password=?', [staffid, password])
row = cursor.fetchone()
if row != None:
print(row[fname]+' '+row[lname])
You may insert any values into this table, I just didn't want the code to look too bulky... Thanks!
I am trying to get the rowid of a username in sqlite3, i have got the basics of it but when ever i run it i get somthing like 'sqlite3.Cursor object at 0x03885660' and it changes every time i run it with the same username. I know it is because i print rowid but i cant find an alternative way.
here is my code:
def sign_in():
username = input("What is your username?")
password = input("What is your password?")
c.execute("SELECT username FROM stuffToPlot")
names = {name[0] for name in c.fetchall()}
if username in names:
rowid = c.execute("SELECT rowid, * FROM stuffToPlot WHERE username = (username)")
print(rowid)
You got to the point where your cursor executes that query, but then you need to tell it what to return from it. Return the first match with that query? return every record? Fetch the data depending on what you need. You can use fetchone(), fetchall() or many other ways to get it.
if username in names:
c.execute("SELECT rowid, * FROM stuffToPlot WHERE username = (username)")
rowid = c.fetchone()
print(rowid)
I am new to Python and am hoping someone can help me figure out how to perform an SQL query on each item in a tuple using Python.
I have a SQL Express server that contains a number of databases for a badge reader system. What I am trying to do is pull the user id's that have scanned into a particular reader, then use those id's to get the actual user names.
Currently, I am able run the query that pulls the user id's and run a query on the other table using just one id. What want to be able to do, and seem to be having an issue figuring out, is running that second query on every user id in the tuple that is created from the first query. Below is the code for the two functions I am currently using.
def get_id():
global cardholder
global cur
cur.execute("SELECT user_id FROM db.table WHERE badgereaderid = 'badgereader1'")
cardholder = []
rows = cur.fetchall()
for row in rows:
if row == None:
break
cardholder.append(row[0])
print(cardholder)
def get_name():
global cardholder
global user
global cur
cur.execute("SELECT FirstName, LastName FROM db.table WHERE user_id= '%s'" % cardholder)
while 1:
row = cur.fetchone()
if row == None:
break
user = row[0] + row[1]
Two possible options
Repeated queries in Python
for user_id in cardholder:
cur.execute("SELECT FirstName, LastName FROM db.table WHERE user_id= '%s'" % user_id)
But why not just pull all the data in the first query?
cur.execute("SELECT a.user_id, b.FirstName, b.LastName FROM db.table1 a left join bd.table2 b on a.user_id = b.user_id WHERE a.badgereaderid = 'badgereader1'")
or, use triple quotes to allow multi-line strings and make the SQL command easier to understand
cur.execute("""SELECT
a.user_id,
b.FirstName,
b.LastName
FROM db.table1 a
left join db.table2 b
on a.user_id = b.user_id
WHERE a.badgereaderid = 'badgereader1'""")
A good practice in Python is to define the data collections outside the function if you intend to use them later on in your code
Try this code:
cardholder_names = []
#pass the cardholder as a param to the function
def get_name(cardholder):
#cur is already defined as a global param, no need to do it twice
cur.execute("SELECT FirstName, LastName FROM db.table WHERE user_id='{0}'".format(cardholder))
return cur.fetchone()
#now use the for loop to iterate over all the cardholders
for holder in cardholders:
cardholder_name = get_name(holder)
cardholder_names.append( {"name" : cardholder_name[0], "surname" : cardholder_name[1]})
I have a form which 4 input fields, when clicking the submit button, I'm fetching results from the database based on those 4 inputs. I'm not forcing the user to enter any input, so I could get all blank inputs or part of them empty. The more inputs the user fills, the results from the database will be more precise.
My problem is how to build the where part of the query dynamically using only the place holders which are populated. here is my query. This way if one of the inputs are blank, the query will not fetch anything while my intention is the opposite: if the input is blank, do not take into account while querying the database.
cursor.execute('''
SELECT name, id, gender, age, address, phones, mails, gender, age, hair_color
FROM persons
WHERE
name = ? AND
id = ? AND
gender = ? AND
age = ?
''', (name_input, id_input, gender_input, age_input))
You could create the WHERE clause dynamically:
sql = "SELECT ... WHERE 1"
parameters = []
if name_input != "":
sql += " AND name = ?"
parameters += [name_input]
if id_input != "":
sql += " AND id = ?"
parameters += [id_input]
...
Alternatively, rewrite the WHERE clause so that empty values are ignored:
SELECT ... WHERE (name = ?1 OR ?1 = '') AND (id = ?2 OR ?2 = '') AND ...
(?n specifies the n-th parameter.)