Python: How do I query to pull data from sqlite3 table? - python

I'm trying to get my code to work but I keep getting this error.
File "C:\Users\mikae\Desktop\Sem 9\CSE115\Assignment 2\Actual\A2
(Version 5).py", line 186, in main
print_table_bookings(conn,booking_data[i])
IndexError: list index out of range
Currently, this is my code.
I'm not done with it, but I'm trying to write out two queries:
Find all the bookings of a specific trainer
Find the gender of the trainer for a type of training.
I've tried a lot of variations but I just can't get the logic down. I would appreciate feedback and any help that I can get.
Code
import sqlite3
from sqlite3 import Error
import os
# Create a connection to the SQLite database via DB file.
def create_connection(db_file):
"""create a database connection to the SQLite database
specified by db_file
:param db_file:database file
:return:Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
# Create tables from SQL statement
def create_table(conn,create_table_sql):
"""create a table from the create_table_sql statement
:param conn:Connection object
:param create_table_sql:a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
# Bookings Table
def print_table_bookings(conn, bookings):
sql=""" INSERT INTO bookings (booking_id,trainer_id,training_id,session_date,session_slot)
VALUES (?,?,?,?,?)"""
c = conn.cursor()
c.execute(sql,bookings)
conn.commit()
return c.lastrowid
# Trainers Table
def print_table_trainers(conn, trainers):
sql=""" INSERT INTO trainers (trainer_id,name,gender,mobile,specialisation,rate)
VALUES (?,?,?,?,?,?)"""
c = conn.cursor()
c.execute(sql,trainers)
conn.commit()
return c.lastrowid
# Trainings Table
def print_table_trainings(conn, trainings):
sql=""" INSERT INTO trainings (training_id,description,duration)
VALUES (?,?,?)"""
c=conn.cursor()
c.execute(sql,trainings)
conn.commit()
return c.lastrowid
# Print Tables
# Print Bookings
def display_bookings(conn):
c=conn.cursor()
c.execute("SELECT * FROM bookings")
rows=c.fetchall()
for row in rows:
print(row)
print("\n")
# Print Bookings
def display_trainers(conn):
c=conn.cursor()
c.execute("SELECT * FROM trainers")
rows=c.fetchall()
for row in rows:
print(row)
print("\n")
# Print Bookings
def display_trainings(conn):
c=conn.cursor()
c.execute("SELECT * FROM trainings")
rows=c.fetchall()
for row in rows:
print(row)
print("\n")
# Query to display Trainer's bookings by Trainer's Name
# NOT DONE
# Main Code
def main():
database = os.path.abspath(os.getcwd()) + "\healthhub.db"
# Create Bookings Table
sql_create_bookings_table = """CREATE TABLE IF NOT EXISTS bookings (
booking_id INTEGER PRIMARY KEY,
trainer_id INTEGER NOT NULL,
training_id TEXT,
session_date INTEGER NOT NULL,
session_slot TEXT NOT NULL,
FOREIGN KEY(trainer_id) REFERENCES trainer(id),
FOREIGN KEY(training_id) REFERENCES training(id)
);"""
# Create Trainer Table
sql_create_trainers_table = """CREATE TABLE IF NOT EXISTS trainers (
trainer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
gender TEXT NOT NULL,
mobile TEXT NOT NULL,
specialisation TEXT NOT NULL,
rate INTEGER NOT NULL
);"""
# Create Trainings Table
sql_create_trainings_table = """CREATE TABLE IF NOT EXISTS trainings (
training_id INTEGER PRIMARY KEY,
description TEXT NOT NULL,
duration INTEGER NOT NULL
);"""
# Create a Database(DB) Connection
conn = create_connection(database)
# Create Tables using def above
if conn is not None:
create_table(conn,sql_create_bookings_table)
create_table(conn,sql_create_trainers_table)
create_table(conn,sql_create_trainings_table)
with conn:
# Error prevention
c=conn.cursor()
c.execute("DELETE FROM bookings;");
c.execute("DELETE FROM trainers;");
c.execute("DELETE FROM trainings;");
# Populating the tables
# Bookings table
booking_1 = (101,'001','0001','01082021','Morning')
booking_2 = (102,'001','0001','01082021','Morning')
booking_3 = (103,'001','0001','01082021','Morning')
booking_4 = (104,'001','0001','01082021','Morning')
booking_5 = (105,'001','0001','01082021','Morning')
booking_6 = (106,'001','0001','01082021','Morning')
booking_7 = (107,'001','0001','01082021','Morning')
booking_8 = (108,'001','0001','01082021','Morning')
booking_9 = (109,'001','0001','01082021','Morning')
booking_10 = (110,'001','0001','01082021','Morning')
# Trainers Table
trainer_1 = (2021,'Gary','Male','91234567','Weight Loss','85')
trainer_2 = (2022,'Bary','Male','91234568','Weight Loss','185')
trainer_3 = (2023,'Mary','Female','91234569','Weight Loss','85')
trainer_4 = (2024,'Stephanie','Female','91234570','Weight Loss','85')
trainer_5 = (2025,'Austin','Male','91234571','Weight Loss','65')
trainer_6 = (2026,'Tynia','Female','91234572','Weight Loss','85')
trainer_7 = (2027,'Oswald','Male','91234573','Weight Loss','55')
trainer_8 = (2028,'Aria','Female','91234574','Weight Loss','45')
trainer_9 = (2029,'Micheal','Male','91234575','Weight Loss','95')
trainer_10 = (2030,'Lily','Female','91234576','Weight Loss','105')
#trainings table
trainings_1 = (3031,'Weight Loss','90')
trainings_2 = (3032,'Cardio','90')
trainings_3 = (3033,'Boxing','90')
trainings_4 = (3034,'Kickboxing','90')
trainings_5 = (3035,'Muay Thai','90')
trainings_6 = (3036,'Kettlebells','90')
trainings_7 = (3037,'Strength training','90')
trainings_8 = (3038,'Yoga','90')
trainings_9 = (3039,'Sparring','90')
trainings_10 = (3040,'Jiu-jitsu','90')
#Loop to write data into table
booking_data = [booking_1,booking_2,booking_3,booking_4,booking_5,booking_6,booking_7,booking_8,booking_9,booking_10]
trainer_data = [trainer_1,trainer_2,trainer_3,trainer_4,trainer_5,trainer_6,trainer_7,trainer_8,trainer_9,trainer_10]
training_data = [trainings_1,trainings_2,trainings_3,trainings_4,trainings_5,trainings_6,trainings_7,trainings_8,trainings_9,trainings_10]
i=0
while i < 20:
print_table_bookings(conn,booking_data[i])
print_table_trainers(conn,trainer_data[i])
print_table_trainings(conn,training_data[i])
i=i+1
#Displaying Table
print_table_bookings(conn)
print()
print_table_trainers(conn)
print()
print_table_trainings(conn)
print()
if __name__=='__main__':
main()

here:
booking_1 = (101,'001','0001','01082021','Morning')
...
booking_data = [booking_1,booking_2,booking_3,booking_4,booking_5,booking_6,booking_7,booking_8,booking_9,booking_10]
...
create_table(conn,booking_data[i])
the booking_data[i] will never be a string containing a SQL instruction. Here you probably want to transform this tuple into a INSERT statement before you execute it.

In main() after you create the tables, you are trying to insert new rows with this loop:
i=0
while i < 20:
create_table(conn,booking_data[i])
create_table(conn,trainer_data[i])
create_table(conn,training_data[i])
i=i+1
in which you use create_table() instead of print_table_bookings(), print_table_trainers() and print_table_trainings().
Change to this:
i=0
while i < 20:
print_table_bookings(conn,booking_data[i])
print_table_trainers(conn,trainer_data[i])
print_table_trainings(conn,training_data[i])
i=i+1

Related

Is it possible to do a for loop with an empty list in Python/sqlite3

I want the user to be able to update values to the database but i want to function to work the same way even if the record doesnt exist in database yet. So i would like to combine INSERT and UPDATE. I was thinking of doing a for loop for the records in the database to see if the record exist and then do an IF statement to decide if i should INSERT or UPDATE. I guess this would work everytime but the first time. If no records exists in the database this doesnt seem to work and i guess thats because i = none when the database is empty.
Example:
conn = sqlite3.connect("database.db")
c = conn.cursor()
c.execute("SELECT *, oid FROM database")
schemalaggning = c.fetchall()
conn.commit()
conn.close()
for i in schemalaggning:
if clicked1.get() and clicked2.get() and clicked10.get() in i:
myUPDATEfunction()
else:
myINSERTfunction2()
How can i solve this problem?
My specific code:
conn = sqlite3.connect("schemalaggning.db")
c = conn.cursor()
c.execute("SELECT *, oid FROM schemalaggning")
schemalaggning = c.fetchall()
conn.commit()
conn.close()
for i in schemalaggning:
if clicked1.get() and clicked2.get() and clicked10.get() in i:
conn = sqlite3.connect("schemalaggning.db")
c = conn.cursor()
c.execute("""UPDATE schemalaggning SET
namn = :namn,
ar = :ar,
vecka = :vecka,
mandag = :mandag,
tisdag = :tisdag,
onsdag = :onsdag,
torsdag = :torsdag,
fredag = :fredag,
lordag = :lordag,
sondag = :sondag
WHERE oid = :oid""",
{
"namn": clicked10.get(),
"ar": clicked1.get(),
"vecka": clicked2.get(),
"mandag": clicked3.get(),
"tisdag": clicked4.get(),
"onsdag": clicked5.get(),
"torsdag": clicked6.get(),
"fredag": clicked7.get(),
"lordag": clicked8.get(),
"sondag": clicked9.get(),
"oid": i[10]
})
conn.commit()
conn.close()
else:
pass
conn = sqlite3.connect("schemalaggning.db")
c = conn.cursor()
c.execute("INSERT INTO schemalaggning VALUES (:namn, :ar, :vecka, :mandag, :tisdag, :onsdag, :torsdag, :fredag, :lordag, :sondag)",
{
"namn": clicked10.get(),
"ar": clicked1.get(),
"vecka": clicked2.get(),
"mandag": clicked3.get(),
"tisdag": clicked4.get(),
"onsdag": clicked5.get(),
"torsdag": clicked6.get(),
"fredag": clicked7.get(),
"lordag": clicked8.get(),
"sondag": clicked9.get()
}
)
conn.commit()
conn.close()
I found a solution for my problem by adding this line before my for loop.
if len(schemalaggning) < 1:
addfunction
else:
for loop as above

Not getting results from database - sqlite 3

I'm trying to insert a large list of English words into a database, and get the results back. However, when I query the database, it is returning nothing. I have omitted the input of the text file into the database for this example, and have only inserted a single string. However this is not showing up in the query either. Here is my code:
import sqlite3
def get_database_connection():
return sqlite3.connect("myDatabase.db")
def commit():
connection = get_database_connection()
connection.commit()
connection.close()
def table_exists(table):
cursor = get_cursor()
cursor.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='{0}' '''.format(table))
my_bool = cursor.fetchone()[0]
return my_bool
def get_cursor():
connection = sqlite3.connect("myDatabase.db")
cursor = connection.cursor()
return cursor
def create_table(table):
if table_exists(table):
return
cursor = get_cursor()
cursor.execute("""CREATE TABLE {0}(
word text
)""".format(table))
def insert_english_words():
#english_words = "english.txt"
#words = process_words(english_words)
table = "english"
create_table(table)
cursor = get_cursor()
cursor.execute("INSERT INTO english VALUES ('HELLO')")
#for word in words:
#cursor.execute("INSERT INTO english VALUES ('{0}')".format(word))
commit()
def get_data():
cursor = get_cursor()
cursor.execute("SELECT * FROM english")
rows = cursor.fetchall()
for row in rows:
print(row)
def run():
get_database_connection()
insert_english_words()
get_data()
print("Done")
run()
You have to commit after creation of table.
Maintain one single connection object across your script lifecycle. You are creating a new connection every single time.

Convert SQLITE 'NoneType in a int python

I'm trying to "SELECT" a value from Db and add this value to another variable, but when I execute this I get this error "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' "
id = input("Digite o id do cartão: ")
cash = int(input("Digite o o valor a ser creditado: "))
dia = 3
sql = 'SELECT saldo FROM carteira where idcartao = ?'
def selectbanco():
c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
row = c.fetchone()
print(row)
row = c.fetchone()
soma = (row) + (cash)
c.execute("UPDATE carteira SET saldo=? WHERE idcartao=?", (soma, id))
connection.commit()
selectbanco()
THIS IS MY COMPLETE CODE
import sqlite3
connection = sqlite3.connect('clientes.db')
c = connection.cursor()
#criação de tabela
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS carteira (idcartao REAL, saldo REAL, data text)')
create_table()
#variaveis
id = input("Digite o id do cartão: ")
cash = int(input("Digite o o valor a ser creditado: "))
dia = 3
sql = 'SELECT saldo FROM carteira where idcartao = ?'
#SELECT E RETORNAR VALOR
def selectbanco():
c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
row = c.fetchone()
print(row)
row = c.fetchone()
##soma = (row + cash)
##print(soma)
c.execute("UPDATE carteira SET saldo=? WHERE idcartao=?", (cash, id))
connection.commit()
selectbanco()
#leitura do banco
def read_data(wordUsed):
for row in c.execute(sql, (wordUsed,)):
print (row)
read_data(id)
connection.close()
You've got two issues here.
The first is that you exhaust your generator by calling row = c.fetchone() twice, without re-executing the query. You can only iterate through your cursor once for each query result; after that, you will need to re-run the query to "refresh" the data and be able to iterate again.
Second, fetchone() will actually return None if you get no matches. This is in contrast to fetchall() that will instead return an empty list ([]) in the case of no matches.
This quick example should illustrate this behaviour:
import sqlite3
# Create a fake database
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS some_table(
something TEXT
)""")
c.execute(""" INSERT INTO some_table VALUES('hello') """)
c.execute("SELECT * FROM some_table")
# We get a match and this will print '(hello,)'
data = c.fetchone()
print(data)
data = c.fetchone()
# If we don't execute the query again but try to use the exhausted generator
# then we'll also get None
print(data)
c.execute("SELECT * FROM some_table WHERE something = 'bye'")
# This will print 'None' because fetchone() didn't get a result
data = c.fetchone()
print(data)
c.execute("SELECT * FROM some_table WHERE something = 'bye'")
# This will print an empty list because fetchall() didn't get a result
data = c.fetchall()
print(data)
c.close()
conn.close()
Even though None and [] are different, they are still falsey so, in the context of your question, you can still convert either response to an integer:
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""create table if not exists some_table(
something TEXT
)""")
c.execute(""" INSERT INTO some_table VALUES('hello') """)
# Get back None or an integer
c.execute(""" SELECT * FROM some_table WHERE something = ?""", ('bye', ))
data = c.fetchone() or 1 # This is where you return an integer instead of None
print(data)
c.close()
conn.close()
I've picked an integer value of 1, maybe you want 0, I'm not sure. The thing to note, though, is that there's two avenues for you to get None or falsey data here, and you're treating them both the same, which isn't great for clarity of code.
You are fetching row twice. You need to remove the second fetch to receive the row.
def selectbanco():
c.execute("SELECT saldo FROM carteira WHERE idcartao=?", (id,))
row = c.fetchone()
print(row)
soma = (row) + (cash)
c.execute("UPDATE carteira SET saldo=? WHERE idcartao=?", (soma, id))
connection.commit()
selectbanco()
The variable gets overwritten because you do not specify a command to execute before fetching (the second time), hence the NoneType.

Python code not creating tables on the database but able to query the results postgres

My usecase is to write create a temp table in the postgres database and fetch records from it and insert into a different table.
The code i used is:
import psycopg2
import sys
import pprint
from __future__ import print_function
from os.path import join,dirname,abspath
import xlrd
import os.path
newlist = []
itemidlist = []
def main():
conn_string = "host='prod-dump.cvv9i14mrv4k.us-east-1.rds.amazonaws.com' dbname='ebdb' user='ebroot' password='*********'"
# print the connection string we will use to connect
# print "Connecting to database" % (conn_string)
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
dealer_id = input("Please enter dealer_id: ")
group_id = input("Please enter group_id: ")
scriptpath = os.path.dirname('__file__')
filename = os.path.join(scriptpath, 'Winco - Gusti.xlsx')
xl_workbook = xlrd.open_workbook(filename, "rb")
xl_sheet = xl_workbook.sheet_by_index(0)
print('Sheet Name: %s' % xl_sheet.name)
row=xl_sheet.row(0)
from xlrd.sheet import ctype_text
print('(Column #) type:value')
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
#print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
num_cols = xl_sheet.ncols
for row_idx in range(0, xl_sheet.nrows): # Iterate through rows
num_cols = xl_sheet.ncols
id_obj = xl_sheet.cell(row_idx, 1) # Get cell object by row, col
itemid = id_obj.value
#if itemid not in itemidlist:
itemidlist.append(itemid)
# execute our Query
'''
cursor.execute("""
if not exists(SELECT 1 FROM model_enable AS c WHERE c.name = %s);
BEGIN;
INSERT INTO model_enable (name) VALUES (%s)
END;
""" %(itemid,itemid))
'''
cursor.execute("drop table temp_mbp1")
try:
cursor.execute("SELECT p.model_no, pc.id as PCid, g.id AS GROUPid into public.temp_mbp1 FROM products p, \
model_enable me, products_clients pc, groups g WHERE p.model_no = me.name \
and p.id = pc.product_id and pc.client_id = %s and pc.client_id = g.client_id and g.id = %s"\
% (dealer_id,group_id)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
cursor.execute("select count(*) from public.temp_mbp1")
# retrieve the records from the database
records = cursor.fetchall()
# print out the records using pretty print
# note that the NAMES of the columns are not shown, instead just indexes.
# for most people this isn't very useful so we'll show you how to return
# columns as a dictionary (hash) in the next example.
pprint.pprint(records)
if __name__ == "__main__":
main()
The try except block in between the program is not throwing any error but the table is not getting created in the postgres database as i see in the data admin.
The output shown is:
Please enter dealer_id: 90
Please enter group_id: 13
Sheet Name: Winco Full 8_15_17
(Column #) type:value
[(3263,)]
Thanks,
Santosh
You didn't commit the changes, so they aren't saved in the database. Add to the bottom, just below the pprint statement:
conn.commit()

Python 3 + SQLite check

Hello
I have a question about SQLite functions, maybe.
So, question:
How to check if name I set in Python is in certain column?
Example:
name = 'John'
Table name = my_table
Column name = users
Code details:
C = conn.cursor()
Please
Use parameter in the query as required. See the attached example for better understanding.
Sample SQLite code for searching value in tables
import sqlite3 as sqlite
import sys
conn = sqlite.connect("test.db")
def insert_single_row(name, age):
try:
age = str(age)
with conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS USER_TABLE(NAME TEXT, AGE INTEGER);")
cursor.execute("INSERT INTO USER_TABLE(NAME, AGE) VALUES ('"+name+"',"+age+")")
return cursor.lastrowid
except:
raise ValueError('Error occurred in insert_single_row(name, age)')
def get_parameterized_row(name):
try:
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM USER_TABLE WHERE NAME = :NAME",
{"NAME":name})
conn.commit()
return cursor.fetchall()
except:
raise ValueError('Error occurred in get_parameterized_row(name)')
if __name__ == '__main__':
try:
return_id = insert_single_row("Shovon", 24)
return_id = insert_single_row("Shovon", 23)
return_id = insert_single_row("Sho", 24)
all_row = get_parameterized_row("Shovon")
for row in all_row:
print(row)
except Exception as e:
print(str(e))
Output:
('Shovon', 24)
('Shovon', 23)
Here I have created a table called USER_TABLE with two attributes: NAME and AGE. Then I inserted several values in the table and searched for a specific NAME. Hope it gives a way to start using SQLite in the project.

Categories

Resources