insert into sqlite table python list - python-3.x

I have a list
mylist = ["18/10/2018","35029128","4T1BF28B6YU095317","TOYOTA","AVALON XL","2000","525","CA","HAYWARD","CA - HAYWARD","3","1200","PDT","Automobile","SEDAN 4D","GRAY","MINOR DENT/SCRATCHES"," ","CA","DQ","YES","D","240778","E","0","0","3.0L 6","Front-wheel Drive","AUTOMATIC","GAS","6","Run & Drive Verified","On Minimum Bid","0","","94545","USA","USD","SF007","N","0","XL","AUCTION DEALER SERVICES LLC"]
and function that inserts this list into database:
def insert_data(self, data):
sql_insert_data = """INSERT INTO copart(salesdata,lot,vin,make,model,year,saleprice,locstate,loccity,yardname,yardNumber,saletime,timezone,vehicle,bodystyle,color,damage,seconddamage,saletitlestate,saletitletype,haskey,lotcond,odometr,odometrtype,estvalue,repair,engine,drive,transmision,fule,cylinders,runsdrives,salestatus,startbid,specialnotes,loczip,loccountry,currency,gridrow,offer,buynow,trim,seller) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""
cur = self.conn.cursor()
try:
cur.execute(sql_insert_data, data)
except Error as e:
print(e)
print("inserted")
when i execute script, it ends without error but no data is inserted in database.
what is wrong in my code?

You need to commit after executing the sql.
Ex:
def insert_data(self, data):
sql_insert_data = """INSERT INTO copart(salesdata,lot,vin,make,model,year,saleprice,locstate,loccity,yardname,yardNumber,saletime,timezone,vehicle,bodystyle,color,damage,seconddamage,saletitlestate,saletitletype,haskey,lotcond,odometr,odometrtype,estvalue,repair,engine,drive,transmision,fule,cylinders,runsdrives,salestatus,startbid,specialnotes,loczip,loccountry,currency,gridrow,offer,buynow,trim,seller) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""
cur = self.conn.cursor()
try:
cur.execute(sql_insert_data, data)
self.conn.commit()
except Error as e:
print(e)
print("inserted")

Related

How to do validation in python?

I am making a gui for employee management system using python tkinter and sqlite3.
In this gui user can add, view, delete amd update employee info.
def save():
con = None
try:
con = connect("pro.db")
cursor = con.cursor()
sql = "insert into Employee values('%d', '%s', '%f')"
id = int(aw_ent_id.get())
name = aw_ent_name.get()
lenstring = False
while not lenstring:
if len(name) >= 2:
lenstring = True
else:
showerror("error","Enter atleast 2 letters")
break
salary = float(aw_ent_salary.get())
cursor.execute(sql%(id, name, salary))
con.commit()
showinfo("success", "record added")
aw_ent_id.delete(0, END)
aw_ent_name.delete(0, END)
aw_ent_salary.delete(0, END)
aw_ent_id.focus()
except Exception as e:
con.rollback()
showerror("issue", e)
finally:
if con is not None:
con.close()
the code is running but i am getting some errors in validating name and salary.
for name i have done validating but its not working. I am getting an error
the data is getting saved even after getting error.
What should i do to make it right?
It is better to:
validate the inputs before saving to database
raise exception if len(name) is less than 2 instead of using while loop checking (actually the while loop is meaningless)
use placeholders to avoid SQL injection
Below is updated save():
# avoid using wildcard import
import tkinter as tk
from tkinter.messagebox import showinfo, showerror
import sqlite3
...
def save():
con = None
try:
# validate inputs
# use emp_id instead of id because id is built-in function
emp_id = int(aw_ent_id.get().strip()) # raise ValueError if not a valid integer
name = aw_ent_name.get().strip()
if len(name) < 2:
raise Exception('Name too short, at least 2 letters')
salary = float(aw_ent_salary.get().strip()) # raise ValueError if not a valid float number
# validations on inputs are passed, try saving to database
sql = 'insert into Employee values (?, ?, ?)' # use placeholders to avoid SQL injection
con = sqlite3.connect('pro.db')
cursor = con.cursor()
cursor.execute(sql, (emp_id, name, salary))
con.commit()
showinfo('Success', 'Employee added')
aw_ent_id.delete(0, tk.END)
aw_ent_name.delete(0, tk.END)
aw_ent_salary.delete(0, tk.END)
aw_ent_id.focus_set()
except Exception as e:
if con:
con.rollback()
showerror("Error", e)
finally:
if con:
con.close()
...

how to run python/djanjo shell as a script

In a tutorial on django we create a simple table run migrations and then go into a shell with the command:
python manage.py shell
from there, in the shell we run the following:
from flights.models import Flight
f = Flight(origin="New York", destination="london", duration=415)
f.save()
I'm trying to figure out how to run these commands from a py file so I created test.py:
from flights.models import Flight
f=Flight(origin="New York",destination="London", duration=415)
f.save()
but get the error Models aren't loaded. How to resolve? I'm definitely a little confused. I am able to update the database from a web served page with the following in my views.py file:
from django.shortcuts import render
from flights.models import Flight
def index(request):
f=Flight(origin="New York",destination="London", duration=415)
f.save()
return render(request, "flights/index.html", {
})
What I am asking is how to update the database directly on the backend. Do I just use standard python sql commands? For instance:
import sqlite3
from sqlite3 import Error
import csv
def sql_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
def sql_create(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)
def sql_insert(conn,sql,data,single):
cur=conn.cursor()
if single:
cur.execute(sql,data)
rowCount = cur.lastrowid
else:
cur.executemany(sql,data)
rowCount = cur.rowcount
conn.commit()
return(rowCount)
def sql_select(conn,sql,data):
cur = conn.cursor()
cur.execute(sql,data)
rows = cur.fetchall()
return rows
def sql_update(conn,sql,data):
cur = conn.cursor()
cur.execute(sql,data)
conn.commit()
def sql_delete(conn,sql,mydata):
print(mydata)
cur = conn.cursor()
cur.execute(sql,mydata)
conn.commit()
def main():
insert = False
db_file = r"/home/saltydog/Database/crud.db"
# create a database connection
conn = sql_connection(db_file)
# create tables
sql_create_price_table = """ CREATE TABLE IF NOT EXISTS prices (
ticker text NOT NULL,
ymd integer,
price real,
PRIMARY KEY(ticker,ymd)
); """
sql_create_price2_table = """ CREATE TABLE IF NOT EXISTS prices2 (
ticker text NOT NULL,
ymd integer,
price real,
PRIMARY KEY(ticker,ymd)
); """
if conn is not None:
# create projects table
sql_create(conn, sql_create_price_table)
sql_create(conn, sql_create_price2_table)
conn.commit()
else:
print("Error! cannot create the database connection.")
if(insert):
sql_insert_record = """insert into prices(ticker,ymd,price)
values(?, ?, ?)"""
cnt = 0
with open('ibm.txt') as f:
reader = csv.reader(f)
for row in reader:
ticker = row[0]
ymd = row[1]
price = row[2]
data = (ticker, ymd, price)
if cnt != 0:
rowid = sql_insert(conn,sql_insert_record,data,True)
print(rowid)
cnt+=1
sql_insert_records = """insert into prices2(ticker,ymd,price)
values(?, ?, ?)"""
data=[]
cnt=0
with open('ibm.txt') as f:
reader = csv.reader(f)
for row in reader:
ticker = row[0]
ymd = row[1]
price = row[2]
if cnt != 0:
data.append((ticker, ymd, price))
cnt+=1
rowid = sql_insert(conn,sql_insert_records,data,False)
print(rowid)
select_records = """select ticker,ymd,price from prices
where ticker = ?
group by price
order by price"""
data=('IBM', )
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
select_records = """select ticker,ymd,price from prices
where ticker = ?
and price > ?"""
data=('IBM',100.0)
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
select_records = """select ticker,ymd,price from prices
where ymd = ?"""
data=(19990527, )
rows = sql_select(conn,select_records,data)
for row in rows:
print(row)
sql_update_records = """update prices
set price = ?
where ymd = ?"""
data = (200.00,19990527)
sql_update(conn,sql_update_records,data)
sql_delete_record = """delete from prices where ymd = ?"""
mydata = (19990528, )
sql_delete(conn,sql_delete_record,mydata)
if __name__ == '__main__':
main()

try to print some table is sqlite3 using python

hi I am a new in python i tried to create a new class that handle with sqlite3 in my read method i try to print some var but is does not print anything can you help me?
here is the code can you fix it and tell me what the problem is
class SQLite_class() :
file_name=''
table_sql_name =''
# con = sqlite3.connect(self.c)
def creat_file_name(self):
self.file_name = input("enter the SQL fille name !")
self.file_name = self.file_name+'.sqlite'
return self.file_name
def conncection(self):
conn = sqlite3.connect(self.file_name)
return conn
def creat_cursor(self):
conn = self.conncection()
cur = conn.cursor()
return cur
def del_table(self):
cur = self.creat_cursor()
cur.execute('DROP TABLE IF EXISTS '+self.table_sql)
def creat_table(self):
cur = self.creat_cursor()
#adding a new option (name of table of more we need to add some {} and use the format function)
cur.execute( '''CREATE TABLE IF NOT EXISTS {} (data TEXT, number INTEGER)'''.format(self.table_sql_name))
self.commiting()
def insrt(self):
cur = self.creat_cursor()
cur.execute('''INSERT INTO {} (data, number) VALUES (?, ?)'''.format(self.table_sql_name) ,('Thunderstruck', 20))
def close(self):
conn = self.conncection()
conn.close()
def commiting(self):
conn = self.conncection()
conn.commit()
def read(self):
cur = self.creat_cursor()
cur.execute('''SELECT data, number FROM {}'''.format(self.table_sql_name))
for row in cur: print(row)
test = SQLite_class()
test.creat_file_name()
test.table_sql_name = 'Track'
test.creat_table()
test.insrt()
test.commiting()
test.read()
test.commiting()
test.close()
This comment # con = sqlite3.connect(self.c) indicates you know what has to be done. I suspect when that didn't work, you went down the rabbit hole of creating connections everywhere. Program should make one db connection. Every time it makes a connection, it loses what came before.
It could declare conn = '', then call conncection() once (and remove it from the other methods; they'll probably need to take conn as an argument). It'll take some try, try again, but it will get you to a better place.
And don't forget to fetch the rows once the SELECT is executed.

Why I am not able to connect to the database

import lot_details, car_details
import numpy as np
import sqlite3
conn = sqlite3.connect('parkingLot.db')
c = conn.cursor()
class Parking(object):
"""
Parking class which has details about parking slots
as well as operation performed on parking are present here
"""
def __init__(self):
self.slots = {}
def create_parking_lot(self, no_of_slots):
try:
c.execute('CREATE TABLE IF NOT EXISTS parkingTable(slot_no REAL, reg_no TEXT, colour TEXT, total_time TEXT,charge TEXT)')
except Exception as ex:
print("Couldn't make a table in DB")
no_of_slots = int(no_of_slots)
if len(self.slots) > 0:
print ("Parking Lot already created")
return
if no_of_slots > 0:
for i in range(1, no_of_slots+1):
temp_slot = lot_details.PSlot(slot_no=i, available=True)
self.slots[i] = temp_slot
print ("Created a parking lot with {} slots" .format(no_of_slots))
else:
print ("Number of slots provided is incorrect.")
return
Using above code I am trying to create a database(which is successful) but now I am not able to create the Table inside it using the above command.
I tried doing it separately its working there. But, I am not able to create it using above code.
Depending on how and where you are creating the Parking object I suspect this could be a scoping issue, either pass the db connection into the constructor or just create it in the constructor itself. Code modified for brevity. The following works for me.
import sqlite3
class Parking(object):
def __init__(self):
self.slots = {}
self.conn = sqlite3.connect('parkingLot.db')
def create_parking_lot(self, no_of_slots):
try:
cur = self.conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS parkingTable(slot_no REAL, reg_no TEXT, colour TEXT, total_time TEXT,charge TEXT)')
except Exception as ex:
print("Couldn't make a table in DB")
return
parking = Parking()
parking.create_parking_lot(5)

Mysql connection from pymysql

I have created two classes class1 and class2 and getting pymysql connection in each classes....
I am updating a mysql database table in class1 and retrieving the same table in class2. But, after updating a row from class1, if I tried to retrieve from class2, the retrieved result is not containing the updated row, instead it is giving the old data...
I have created the class Database and connecting to the database. If I want to execute a query in class1 or class2, I will just call the function in Database and passing the SQL query to execute and will return the result to respective classes.
database.py file is shown below.
import pymysql
class Database:
def __init__(self): # WORKS
self.db = pymysql.connect(host="localhost", user="root", passwd="root", db="invexl")
self.cur = self.db.cursor()
def create(self, sql):
try:
self.cur.execute(sql)
return self.db.commit()
except Exception as e:
print(str(e))
self.db.rollback()
return str(e)
def retrieve(self, sql):
try:
done = self.cur.execute(sql)
result = self.cur.fetchall()
return result
except Exception as e:
print(str(e))
self.db.rollback()
return str(e)
def update(self, sql):
try:
self.cur.execute(sql)
return self.db.commit()
except Exception as e:
print(str(e))
self.db.rollback()
return str(e)
def delete(self, sql):
try:
self.cur.execute(sql)
return self.db.commit()
except Exception as e:
print(str(e))
self.db.rollback()
return str(e)

Resources