No result in the table after executing INSERT in Python SQLite - python

I used Python and the package SQLite to create table and insert data into the table. However, there is nothing in the table after I fired the execution. Can anyone help me figure it out? Thanks.
def conSqlite():
conn = sqlite3.connect('C:\\Users\jet.cai\Documents\Logsitic.db')
json_path = r'C:\Users\jet.cai\PycharmProjects\VJSF\txtToJson.json'
try:
create_table = ('''
CREATE TABLE IF NOT EXISTS CODE2
(Delivery TEXT,
Customer_Name NCHAR(50),
Shipment_Priority TEXT
)''')
conn.execute(create_table)
except:
print("Table Failed")
return False
with open(json_path, 'r') as jsonf:
lines = json.load(jsonf)
for line in lines:
sql = "insert into CODE2(Delivery,Customer_Name,Shipment_Priority) values('%s','%s','%s')"%(line['Delivery'],line['Customer Name'],line['Shipment Priority'])
conn.execute(sql)
# No results can be selected out
df = pd.read_sql("select Delivery from CODE2", conn)
print(df)

Related

Getting error while inserting data in sqlite3

I am new to Python and started off with sqlite.
I have two csv transaction.csv and users.csv from where I am reading the data and writing to the sqlite database.Below is the snippet
import csv
import sqlite3 as db
def readCSV_users():
with open('users.csv',mode='r') as data:
dr = csv.DictReader(data, delimiter=',')
users_data = [(i['user_id'], i['is_active']) for i in dr if i['is_active']=='True']
#---------------------
return users_data
def readCSV_transactions():
with open('transactions.csv',mode='r') as d:
dr = csv.DictReader(d, delimiter=',')
trans_data = [(i['user_id'], i['is_blocked'],i['transaction_amount'],i['transaction_category_id']) for i in dr if i['is_blocked']=='False']
#---------------------
return trans_data
def SQLite_connection(database):
try:
# connect to the database
conn = db.connect(database)
print("Database connection is established successfully!")
conn = db.connect(':memory:')
print("Established database connection to a database\
that resides in the memory!")
cur = conn.cursor()
return cur,conn
except exception as Err:
print(Err)
def dbQuery(users_data,trans_data,cur,conn):
try:
cur.executescript(""" CREATE TABLE if not exists users(user_id text,is_active text);
CREATE TABLE if not exists transactions(user_id text,is_blocked text,transaction_amount text,transaction_category_id text);
INSERT INTO users VALUES (?,?),users_data;
INSERT INTO transactions VALUES (?,?,?,?),trans_data""")
conn.commit()
a=[]
rows = curr.execute("SELECT * FROM users").fetchall()
for r in rows:
a.append(r)
return a
except Err:
print(Err)
finally:
conn.close()
if __name__ == "__main__":
database='uit'
users_data=readCSV_users()
trans_data=readCSV_transactions()
curr,conn=SQLite_connection(database)
print(dbQuery(users_data,trans_data,curr,conn))
But I am facing below error.I believe the ? is throwing the error in executescript
cur.executescript(""" CREATE TABLE if not exists users(user_id text,is_active text);
sqlite3.OperationalError: near "users_data": syntax error
Any pointers to resolve this?
Putting users_data directly in query is wrong. It treats it as normal string.
But it seems executescript can't use arguments.
You would have to put values directly in place of ?.
Or you have to use execute()
cur.execute("INSERT INTO users VALUES (?,?);", users_data)
cur.execute("INSERT INTO transactions VALUES (?,?,?,?)", trans_data)

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

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

"where" in mysql table

I am using a code that is using mysql. I am very new in mysql so I would be thankful if you could help. My input is a huge dumpfile of wikipediapages in xml bz2 format. The input format is some text files extracted from that xml file with this format:
<doc id="12" url="https://en.wikipedia.org/wiki?curid=12" title="Anarchism"> text... </doc>
the only parts that connects the program to sql is as follows:
def read_in_STOP_CATS(f_n = "/media/sscepano/Data/Wiki2015/STOPCAT/STOP_CATS.txt"):
s = []
f = open(f_n, "r")
for line in f:
s.append(line.rstrip().lower())
return s
def connect_2_db():
try:
cnx = mysql.connector.connect(user='test', password='test',
host='127.0.0.1',
database='wiki_category_links')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
return cnx
def articles_selected(aid):
global cnx
global STOP_CATS
cursor = cnx.cursor(buffered=True)
cursor.execute("SELECT * FROM categorylinks where cl_from = " + str(aid))
row = cursor.fetchone()
while row is not None:
#print(row)
cat = row[1].lower()
#print cat
for el in STOP_CATS:
if el in cat:
return False
row = cursor.fetchone()
return True
cnx = connect_2_db()
STOP_CATS = read_in_STOP_CATS()
TITLE_WEIGHT = 4
my problem is that right now I do not know how should I connect to mysql to be able to run the code and the main prob;lem is that I do not know what is categorylinks in the code? That should be the name of my sql table? Does it mean that I need to make an sql table with this name and import all my text file in this one table?
what does 'where' means in this line also????
As RiggsFolly said, you need to get something like WHERE cl_from = 'some string'
You could do it this way:
cursor.execute("SELECT * FROM categorylinks where cl_from ='" + str(aid)+"'")
But it is better to use prepared statements like this one:
select_stmt = "SELECT * FROM categorylinks where cl_from = %(aid)s"
cursor.execute(select_stmt, { 'aid':str(aid) })
So in your code you have:
A database named wiki_category_links
In that database you have a table called categorylinks
And the select you have means that you are going to get, from table categorylinks, all rows that have the column cl_from equal to the value of aid variable.

Writing column names to file

I have an SQLite DB file and I am parsing the data from each column in a table of the db to a .txt file. At the moment it is writing the column contents to the file but it won't pull the column names and write those. How can I go about it as I have tried to use this guide Is there a way to get a list of column names in sqlite? but i cannot seem to get it to work. Here is my code with an attempt at pulling the column names from the table.
import sqlite3
from sqlite3 import Error
# create a database connection to the SQLite database specified by the db_file
def create_connection(db_file,detect_types=sqlite3.PARSE_DECLTYPES):
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
# Query specific rows in the sms table
def select_data(conn):
cur = conn.cursor()
cur.execute("SELECT _id, address, strftime('%d-%m-%Y', date / 1000, 'unixepoch'),read, type, body, seen FROM sms")
print("Writing the contents of the sms table to an evidence file")
print("\t")
# Trying to pull out column names from db table
def get_col_names():
conn = sqlite3.connect("mmssms.db")
c = conn.cursor()
c.execute("SELECT _id, address, strftime('%d-%m-%Y', date / 1000, 'unixepoch'),read, type, body, seen FROM sms")
return [member[0] for member in c.description]
# Write the data to a smsEvidence.txt file
with open('EvidenceExtractionFiles/smsInfo.txt', 'a+') as f:
rows = cur.fetchall()
for row in rows:
#print(row)
f.write("%s\n" % str(row))
print("SMS Data is written to the evidence File")
# path to where the db files are stored
def main():
database = "H:\College Fourth Year\Development Project\Final Year Project 2018\mmssms.db"
# create a database connection
conn = create_connection(database)
with conn:
# print("Query specific columns")
select_data(conn)
# close db connection
if(conn):
conn.close()
print("Database closed")
if __name__ == '__main__':
main()
You may use cursor.description which holds info about the column names:
[ ... ]
cur = cursor.execute('SELECT * FROM test_table LIMIT 100')
col_names = [ name[0] for name in cur.description ]
print (col_names)
[ ... ]

Insert data instead of drop table into mysql

I'm attempting to get a python script to insert data into a database without having it drop the table first.. I'm sure this isn't hard to do but I can't seem to get the code right..
Here is the full python script..
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import hashlib
import time
import MySQLdb
#Dont forget to fill in PASSWORD and URL TO saveTemp (twice) in this file
sensorids = ["28-000004944b63", "28-000004c01b2c"]
avgtemperatures = []
for sensor in range(len(sensorids)):
temperatures = []
for polltime in range(0,3):
text = '';
while text.split("\n")[0].find("YES") == -1:
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave")
# Read all of the text in the file.
text = tfile.read()
# Close the file now that the text has been read.
tfile.close()
time.sleep(1)
# Split the text with new lines (\n) and select the second line.
secondline = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperatures.append(temperature / 1000 * 9.0 / 5.0 + 32.0)
avgtemperatures.append(sum(temperatures) / float(len(temperatures)))
print avgtemperatures[0]
print avgtemperatures[1]
#connect to db
db = MySQLdb.connect("localhost","user","password","temps" )
#setup cursor
cursor = db.cursor()
#create temps table
cursor.execute("DROP TABLE IF EXISTS temps")
sql = """CREATE TABLE temps (
temp1 FLOAT,
temp2 FLOAT )"""
cursor.execute(sql)
#insert to table
try:
cursor.execute("""INSERT INTO temps VALUES (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))
db.commit()
except:
db.rollback()
#show table
cursor.execute("""SELECT * FROM temps;""")
print cursor.fetchall()
((188L, 90L),)
db.close()
This is the part I need assistance with..
If I have it drop the table it works fine but I don't want it to drop the table, just insert the new data into the same table.
#connect to db
db = MySQLdb.connect("localhost","user","pasword1","temps" )
#setup cursor
cursor = db.cursor()
#create temps table
cursor.execute("DROP TABLE IF EXISTS temps")
sql = """CREATE TABLE temps (
temp1 FLOAT,
temp2 FLOAT )"""
cursor.execute(sql)
#insert to table
try:
cursor.execute("""INSERT INTO temps VALUES (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))
db.commit()
except:
db.rollback()
#show table
cursor.execute("""SELECT * FROM temps;""")
print cursor.fetchall()
((188L, 90L),)
db.close()
You shouldn`t have to drop a table each time you want to enter data. In fact, it defeats the whole purpose of the database since you will remove all the previous data each time you run your script.
You should ask to create the table but only if it does not exists. Use the following.
sql = """CREATE TABLE IF NOT EXISTS temps (
temp1 FLOAT,
temp2 FLOAT )"""
cursor.execute(sql)
I've had this problem with updating. Try adding COMMIT to the end of your sql. I use psycopg2 to connect to a postgresql database. Here is an example.
def simple_insert():
sql = '''INSERT INTO films VALUES ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes'); COMMIT;'''
try:
conn = psycopg2.connect(database)
cur = conn.cursor()
cur.execute(sql)
except:
raise
I think your problem is your not saving the transaction and the COMMIT command should fix it.

Categories

Resources