SlideShare a Scribd company logo
PYTHON INTERACTION
WITH SQLITE
BY AMIT PATEL
IMPORTING MODULE
• “Import” statement imports the sqlite3 module in the program.
• Using the classes and methods defined in the sqlite3 module we can communicate with the
SQLite database.
• Import in python is similar to #include header_file in C/C++.
• Python modules can get access to code from another module by importing the file/function
using import.
• When the import is used, it searches for the module initially in the local scope by calling
__import__() function.
import module_name
IMPORTING MODULE
• For example:
import math
print(math.pi)
import module_name.member_name
• In the above code module, math is imported, and its variables can be accessed by considering
it to be a class and pi as its object.
• The value of pi is returned by __import__().
• pi as a whole can be imported into our initial code, rather than importing the whole module.
IMPORTING MODULE
• For example:
from math import pi
# Note that in the above example,
# we used math.pi. Here we have used
# pi directly.
print(pi)
from module_name import *
• In the above code module, math is not imported, rather just pi has been imported as a variable.
• All the functions and constants can be imported using *.
CONNECT() AND EXECUTE()
• Connecting to the SQLite Database can be established using the connect() method, passing the
name of the database to be accessed as a parameter.
• If that database does not exist, then it’ll be created.
sqliteConnection = sqlite3.connect('sql.db')
• But what if you want to execute some queries after the connection is being made.
• For that, a cursor has to be created using the cursor() method on the connection instance,
which will execute our SQL queries.
cursor = sqliteConnection.cursor()
print('DB Init')
CONNECT() AND EXECUTE()
• The SQL query to be executed can be written in form of a string, and then executed by calling
the execute() method on the cursor object.
• Then, the result can be fetched from the server by using the fetchall() method, which in this
case, is the SQLite Version Number.
query = 'SQL query;'
cursor.execute(query)
result = cursor.fetchall()
print('SQLite Version is {}'.format(result))
FETCHALL( ) & FETCHONE( )
• fetchall(), fetchmany(), and fetchone( ) to retrieve data from MySQL, PostgreSQL, SQLite
database.
• cursor.fetchall() fetches all the rows of a query result. It returns all the rows as a list of tuples.
An empty list is returned if there is no record to fetch.
• cursor.fetchmany(size) returns the number of rows specified by size argument. When called
repeatedly, this method fetches the next set of rows of a query result and returns a list of
tuples. If no more rows are available, it returns an empty list.
• cursor.fetchone() method returns a single record or None if no more rows are available.
FETCHALL( )
To fetch all rows from a database table, you need to follow these simple steps: –
• Create a database Connection from Python.
• Define the SELECT query. Here you need to know the table and its column details.
• Execute the SELECT query using the cursor.execute() method.
• Get resultSet (all rows) from the cursor object using a cursor.fetchall().
• Iterate over the ResultSet using for loop and get column values of each row.
• Close the Python database connection.
• Catch any SQL exceptions that may come up during the process.
FETCHALL( )
import sqlite3
def getAllRows():
try:
connection = sqlite3.connect('SQLite_Python.db')
cursor = connection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT * from database_developers"""
cursor.execute(sqlite_select_query)
records = cursor.fetchall()
FETCHALL( )
print("Total rows are: ", len(records))
print("Printing each row")
for row in records:
print("Id: ", row[0])
print("Name: ", row[1])
print("Email: ", row[2])
print("Salary: ", row[3])
print("n")
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from table", error)
finally:
if connection:
connection.close()
print("The Sqlite connection is closed")
getAllRows() //function call to get all row
FETCHMANY()
• Retrieve a few rows from a table.
• Here size is the number of rows to be retrieved.
• This method fetches the next set of rows of a query result and returns a list of tuples. If no
more rows are available, it returns an empty list.
• Cursor’s fetchmany() method returns the number of rows specified by size argument. Default
value is 1. If the specified size is 100, then it returns 100 rows.
• Let try to fetch 3 rows from table using a cursor.fetchmany(size)
rows = cursor.fetchmany([size=cursor.arraysize])
FETCHMANY( )
import sqlite3
def getlimitedRows(size):
try:
connection = sqlite3.connect('SQLite_Python.db')
cursor = connection.cursor()
print("Connected to database")
sqlite_select_query = """SELECT * from database_developers"""
cursor.execute(sqlite_select_query)
records = cursor.fetchmany(size)
FETCHMANY( )
print("Fetching Total ", size," rows")
print("Printing each row")
for row in records:
print("Id: ", row[0])
print("Name: ", row[1])
print("Email: ", row[2])
print("Salary: ", row[3])
print("n")
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from table", error)
finally:
if (connection):
connection.close()
print("The Sqlite connection is closed")
getlimitedRows(3) // function call to get 3 row
FETCHONE()
• Retrieve only single rows from a table.
• It can return a none if no rows are available in the result set.
• cursor.fetchone() increments the cursor position by one and return the next row.
rows = cursor.fetchone( )
FETCHONE( )
import sqlite3
def getSingleRows():
try:
connection = sqlite3.connect('SQLite_Python.db')
cursor = connection.cursor()
print("Connected to database")
sqlite_select_query = """SELECT * from database_developers"""
cursor.execute(sqlite_select_query)
print("Fetching single row")
FETCHONE( )
record = cursor.fetchone()
print(record)
print("Fetching next row")
record = cursor.fetchone()
print(record)
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from table", error)
finally:
if connection:
connection.close()
print("The Sqlite connection is closed")
getSingleRows() // get single row
CRUD OPERATION USING EXECUTE( )
• Retrieve only single rows from a table.
• It can return a none if no rows are available in the result set.
• cursor.fetchone() increments the cursor position by one and return the next row.
rows = cursor.fetchone( )
STEPS TO CONNECT WITH SQLITE DATABASE
• Import sqlite3 module.
• Use the connect( ) method.
• Use the cursor( ) method.
• Use the execute( ) method.
• Extract result using fetchall( ).
• Close cursor and connection objects.
• Catch database exception if any that may occur during connection process.
CREATE SQLITE TABLE FROM PYTHON
Steps for create a table in SQLite from Python: –
• Connect to SQLite using a sqlite3.connect().
• Prepare a create table query.
• Execute the query using a cursor.execute(query)
CREATE SQLITE TABLE FROM PYTHON
import sqlite3
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
sqlite_create_table_query = '''CREATE TABLE SqliteDb_developers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email text NOT NULL UNIQUE,
joining_date datetime,
salary REAL NOT NULL);'''
FETCHONE( )
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
cursor.execute(sqlite_create_table_query)
sqliteConnection.commit()
print("SQLite table created")
cursor.close()
except sqlite3.Error as error:
print("Error while creating a sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("sqlite connection is closed")
INSERT OPERATION
• Connect to SQLite from Python
• Define a SQL Insert query
• Get Cursor Object from Connection
• Execute the insert query using execute() method
• Commit your changes
• Get the number of rows affected
• Verify result using the SQL SELECT query
• Close the cursor object and database connection object
INSERT
import sqlite3
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
sqlite_insert_query = """INSERT INTO SqliteDb_developers
(id, name, email, joining_date, salary)
VALUES
(1,'James','james@pynative.com','2019-03-17',8000)"""
INSERT
count = cursor.execute(sqlite_insert_query)
sqliteConnection.commit()
print("Record inserted successfully into SqliteDb_developers table ", cursor.rowcount)
cursor.close()
except sqlite3.Error as error:
print("Failed to insert data into sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
INSERT OPERATION USING PYTHON VARIABLE
• Sometimes we need to insert a Python variable value into a table’s column.
• This value can be anything, including integer, string, float, and DateTime.
• For example, in the registration form person enter his/her details. You can take those values
in Python variables and insert them into the SQLite table.
• We use a parameterized query to insert Python variables into the table.
• Using a parameterized query, we can pass python variables as a query parameter in which
placeholders (?)
INSERT OPERATION USING PYTHON VARIABLE
import sqlite3
def insertVaribleIntoTable(id, name, email, joinDate, salary):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_insert_with_param = """INSERT INTO SqliteDb_developers
(id, name, email, joining_date, salary)
VALUES (?, ?, ?, ?, ?);"""
INSERT OPERATION USING PYTHON VARIABLE
data_tuple = (id, name, email, joinDate, salary)
cursor.execute(sqlite_insert_with_param, data_tuple)
sqliteConnection.commit()
print("Python Variables inserted successfully into SqliteDb_developers table")
cursor.close()
except sqlite3.Error as error:
print("Failed to insert Python variable into sqlite table", error)
INSERT OPERATION USING PYTHON VARIABLE
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
insertVaribleIntoTable(2, 'Joe', 'joe@pynative.com', '2019-05-19', 9000)
insertVaribleIntoTable(3, 'Ben', 'ben@pynative.com', '2019-02-23', 9500)
INSERT MULTIPLE ROW
• Sometimes we need to insert multiple rows into the table in a single insert query.
• For example, You wanted to add all records from the CSV file into the SQLite table.
• Instead of executing the INSERT query every time to add each record, you can perform a
bulk insert operation in a single query using a cursor’s executemany() function.
• The executemany() method takes two arguments SQL query and records to update.
INSERT MULTIPLE ROW
import sqlite3
def insertMultipleRecords(recordList):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_insert_query = """INSERT INTO SqliteDb_developers
(id, name, email, joining_date, salary)
VALUES (?, ?, ?, ?, ?);"""
INSERT MULTIPLE ROW
cursor.executemany(sqlite_insert_query, recordList)
sqliteConnection.commit()
print("Total", cursor.rowcount, "Records inserted successfully into SqliteDb_developers table")
sqliteConnection.commit()
cursor.close()
except sqlite3.Error as error:
print("Failed to insert multiple records into sqlite table", error)
INSERT MULTIPLE ROW
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
recordsToInsert = [(4, 'Jos', 'jos@gmail.com', '2019-01-14', 9500),
(5, 'Chris', 'chris@gmail.com', '2019-05-15', 7600),
(6, 'Jonny', 'jonny@gmail.com', '2019-03-27', 8400)]
insertMultipleRecords(recordsToInsert)
UPDATE
• Most of the time, we need to update a table with some runtime values.
• For example, when users update their profile or any other details through a user interface,
we need to update a table with those new values.
• In such cases, It is always best practice to use a parameterized query.
• The parameterized query uses placeholders (?) inside SQL statements that contain input
from users.
• It helps us to update runtime values and prevent SQL injection concerns.
UPDATE
import sqlite3
def updateSqliteTable(id, salary):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sql_update_query = """Update SqliteDb_developers set salary = ? where id = ?"""
data = (salary, id)
cursor.execute(sql_update_query, data)
UPDATE
sqliteConnection.commit()
print("Record Updated successfully")
cursor.close()
except sqlite3.Error as error:
print("Failed to update sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The sqlite connection is
closed")
updateSqliteTable(3, 7500)
UPDATE
• We used two placeholders in the update query, one for the salary column and the other is
for the id column.
• Next, We prepared a data tuple by specifying two Python variables in sequential order.
• Next, we passed the SQL update query and data tuple to the cursor.execute() method.
Remember variables order in the tuple is sequential as per column placeholders order.
UPDATE MULTIPLE ROW USING CURSOR
• In the above example, we have used execute() method of cursor object to update a single
record. But sometimes, we need to update multiple rows of the SQLite table.
• For example, you want to increase the salary of developers by 20%.
• Instead of executing the UPDATE query every time to update each record, you can perform
bulk update operations in a single query using the cursor.executemany() method.
• The executemany(query, seq_param) method accepts the following two parameters
• SQL query
• list of records to be updated.
UPDATE MULTIPLE ROW USING CURSOR
import sqlite3
def updateMultipleRecords(recordList):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_update_query = """Update SqliteDb_developers set salary = ? where id = ?"""
cursor.executemany(sqlite_update_query, recordList)
UPDATE MULTIPLE ROW USING CURSOR
sqliteConnection.commit()
print("Total", cursor.rowcount, "Records updated
successfully")
sqliteConnection.commit()
cursor.close()
except sqlite3.Error as error:
print("Failed to update multiple records of sqlite table", error)
UPDATE MULTIPLE ROW USING CURSOR
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
records_to_update = [(9700, 4), (7800, 5), (8400, 6)]
updateMultipleRecords(records_to_update)
UPDATE MULTIPLE ROW USING CURSOR
• We prepared the SQLite update query with two placeholders (“salary” and “Id” column ) and
a list of records to update in tuple format.
• Each element of a list is nothing but a tuple for each row. Each tuple contains two values, i.e.,
salary and id of a developer.
• We passed SQLite update query and record list to executemany() as arguments.
• To get to know the number of records updated, we used a cursor.rowcount function.
DELETE
• In order to delete data in the SQLite database from a Python program, you use the following
steps:
• First, establish a connection the SQLite database by creating a Connection object using the
connect() function.
• Second, to execute a DELETE statement, you need to create a Cursor object using the
cursor() method of the Connection object.
• Third, execute the DELETE statement using the execute() method of the Cursor object. In
case you want to pass the arguments to the statement, you use a question mark (?) for each
argument.
EXAMPLE:
#import the module.
import sqlite3;
#Use the connect( ) method for open or create a database
con = sqlite3.connect("MyDB.db")
print("Database Created")
#Use the cursor( ) method using connection object
cur = con.cursor()
#create a table using execute() method
createtbl = "create table test(ID int primary key, name text not null, total int not null)"
con.execute(createtbl)
print("Table Created")
INSERT DATA
#insert the data using execute() method
insertquery = "insert into test values(1,'Nidhi',78);"
con.execute(insertquery)
print("data inserted", con.total_changes)
#take input from user to insert data into table.
id1 = input("Enter ID:")
name = input("Enter Name:")
total = input("Enter Total:")
con.execute("insert into test values(?,?,?)", (id1,name,total))
print("data inserted", con.total_changes)
INSERT MULTIPLE DATA
#insert multiple data into table using executemany() method.
records = [(3,'mnp',33), (4,'pqr',67),(6,'wxy',88)]
con.executemany("insert into test values(?,?,?)", records)
print("data inserted", con.total_changes)
UPDATE DATA
#update the data using execute() method
updatequery = "update test set total=90 where id=1"
con.execute(updatequery)
print("data updated", con.total_changes)
#take input from user to update data into table.
idtoupadte = input("Enter id:")
con.execute("update test set total=99 where id=?", idtoupadte)
print("data updated", con.total_changes)
UPDATE DATA
#take multiple input from user to update data.
updatename=input("enter update name:")
updateid = input("enter id:")
con.execute("update test set name=? where id=?",(updatename,updateid))
print("data updated", con.total_changes)
#update multiple data into table using executemany() method.
updatedata = [('priya',55,3),
('payal', 56,4),
('pritam',57,6)]
con.executemany("update test set name=?, total=? where id=?",updatedata)
print("data updated", con.total_changes)
DELETE DATA
#delete data from table using execute() method
deletequery = "delete from test where id=1"
con.execute(deletequery)
print("data deleted", con.total_changes)
#take input from user to delete data into table.
deleteid = input("Enter id:")
con.execute("delete from test where id=?",deleteid)
print("data deleted", con.total_changes)
SELECT DATA
#select data from table usinf cursor() object and print the data using for loop.
data = con.execute("SELECT * from test")
for row in data:
print ("ID = ", row[0])
print ("NAME = ", row[1])
print ("TOTAL = ", row[2])
SELECT DATA- FETCHALL()
#use the fetchall() method to get all data.
cur.execute("select * from test")
data1 = cur.fetchall()
for row in data1:
print ("ID = ", row[0])
print ("NAME = ", row[1])
print ("TOTAL = ", row[2])
SELECT DATA- FETCHONE() AND FECTHMANY()
#use fetchone() to get a single record
cur.execute("select * from test")
data2 =cur.fetchone()
print(data2)
#use fetchmany() method to get number of data record
cur.execute("select * from test")
data3 = cur.fetchmany(2)
print(data3)
EXAMPLE
#print the list of tables of database
print("List of tables:")
cur.execute("SELECT * FROM sqlite_master WHERE type='table';")
print(cur.fetchall())
#print the structure of a table.
a= con.execute("PRAGMA table_info('test')")
print("nTable structure of test:", a)
for i in a:
print(i)
EXAMPLE
#print the foreign keys in database
rows=cur.execute('pragma foreign_keys')
for r in rows:
print (r)
#use commit() method at last to save changes in your table
con.commit()
#use close() method to close the connection
con.close()
WHAT IS EXCEPTION?
• An exception is an event, which occurs during the execution of a program that disrupts the
normal flow of the program's instructions. In general, when a Python script encounters a
situation that it cannot cope with, it raises an exception. An exception is a Python object that
represents an error.
• When a Python script raises an exception, it must either handle the exception immediately
otherwise it terminates and quits.
HANDLING AN EXCEPTION?
• If you have some suspicious code that may raise an exception, you can defend your
program by placing the suspicious code in a try: block. After the try: block, include an
except: statement, followed by a block of code which handles the problem as elegantly as
possible.
HANDLING AN EXCEPTION?
• The try block lets you test a block of code for errors.
• The except block lets you handle the error.
• The else block lets you execute code when there is no error.
• The finally block lets you execute code, regardless of the result of the try- and except blocks.
• When an error occurs, or exception as we call it, Python will normally stop and generate an
error message.
• These exceptions can be handled using the try statement:
TRY EXCEPT
• This statement will raise an error, because x is not defined:
print(x)
• If we write below statement then it print a error statement rather than throw a error.
try:
print(x)
except:
print("An exception occurred")
Output:
An exception occurred
MANY EXCEPTION
• You can define as many exception blocks as you want, e.g. if you want to execute a special block
of code for a special kind of error:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
Output:
Variable x is not defined
• Print one message if the try block raises a NameError and another for other errors:
ELSE
• You can use the else keyword to define a block of code to be executed if no errors were
raised:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
Output:
Hello
Nothing went wrong
FINALLY
• The finally block, if specified, will be executed regardless if the try block raises an error or
not. try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
Output:
Something went wrong
The 'try except' is finished
Ad

More Related Content

Similar to 3 PYTHON INTERACTION WITH SQLITE (concept of python) (20)

The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31
Mahmoud Samir Fayed
 
Sqlite3 databases
Sqlite3 databasesSqlite3 databases
Sqlite3 databases
Mohamed Essam
 
MySql Interface database in sql python my.pptx
MySql Interface database in sql python my.pptxMySql Interface database in sql python my.pptx
MySql Interface database in sql python my.pptx
UshimArora
 
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptxPYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
9 Python programming notes for ktu physics and computer application semester 4
9 Python programming notes for ktu  physics and computer application semester 49 Python programming notes for ktu  physics and computer application semester 4
9 Python programming notes for ktu physics and computer application semester 4
ebindboby1
 
SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
Mahmoud Samir Fayed
 
interface with mysql.pptx
interface with mysql.pptxinterface with mysql.pptx
interface with mysql.pptx
KRITIKAOJHA11
 
24. SQL .pdf
24. SQL .pdf24. SQL .pdf
24. SQL .pdf
Bhavya103897
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
hameedkhan2017
 
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189
Mahmoud Samir Fayed
 
015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12
Shuvanth
 
The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
Mahmoud Samir Fayed
 
Python database access
Python database accessPython database access
Python database access
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Sql injection
Sql injectionSql injection
Sql injection
Nitish Kumar
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.8 book - Part 34 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31The Ring programming language version 1.4.1 book - Part 8 of 31
The Ring programming language version 1.4.1 book - Part 8 of 31
Mahmoud Samir Fayed
 
MySql Interface database in sql python my.pptx
MySql Interface database in sql python my.pptxMySql Interface database in sql python my.pptx
MySql Interface database in sql python my.pptx
UshimArora
 
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptxPYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
9 Python programming notes for ktu physics and computer application semester 4
9 Python programming notes for ktu  physics and computer application semester 49 Python programming notes for ktu  physics and computer application semester 4
9 Python programming notes for ktu physics and computer application semester 4
ebindboby1
 
SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
Mahmoud Samir Fayed
 
interface with mysql.pptx
interface with mysql.pptxinterface with mysql.pptx
interface with mysql.pptx
KRITIKAOJHA11
 
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.6 book - Part 31 of 189
Mahmoud Samir Fayed
 
015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12
Shuvanth
 
The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
Mahmoud Samir Fayed
 

Recently uploaded (20)

MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Ad

3 PYTHON INTERACTION WITH SQLITE (concept of python)

  • 2. IMPORTING MODULE • “Import” statement imports the sqlite3 module in the program. • Using the classes and methods defined in the sqlite3 module we can communicate with the SQLite database. • Import in python is similar to #include header_file in C/C++. • Python modules can get access to code from another module by importing the file/function using import. • When the import is used, it searches for the module initially in the local scope by calling __import__() function. import module_name
  • 3. IMPORTING MODULE • For example: import math print(math.pi) import module_name.member_name • In the above code module, math is imported, and its variables can be accessed by considering it to be a class and pi as its object. • The value of pi is returned by __import__(). • pi as a whole can be imported into our initial code, rather than importing the whole module.
  • 4. IMPORTING MODULE • For example: from math import pi # Note that in the above example, # we used math.pi. Here we have used # pi directly. print(pi) from module_name import * • In the above code module, math is not imported, rather just pi has been imported as a variable. • All the functions and constants can be imported using *.
  • 5. CONNECT() AND EXECUTE() • Connecting to the SQLite Database can be established using the connect() method, passing the name of the database to be accessed as a parameter. • If that database does not exist, then it’ll be created. sqliteConnection = sqlite3.connect('sql.db') • But what if you want to execute some queries after the connection is being made. • For that, a cursor has to be created using the cursor() method on the connection instance, which will execute our SQL queries. cursor = sqliteConnection.cursor() print('DB Init')
  • 6. CONNECT() AND EXECUTE() • The SQL query to be executed can be written in form of a string, and then executed by calling the execute() method on the cursor object. • Then, the result can be fetched from the server by using the fetchall() method, which in this case, is the SQLite Version Number. query = 'SQL query;' cursor.execute(query) result = cursor.fetchall() print('SQLite Version is {}'.format(result))
  • 7. FETCHALL( ) & FETCHONE( ) • fetchall(), fetchmany(), and fetchone( ) to retrieve data from MySQL, PostgreSQL, SQLite database. • cursor.fetchall() fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if there is no record to fetch. • cursor.fetchmany(size) returns the number of rows specified by size argument. When called repeatedly, this method fetches the next set of rows of a query result and returns a list of tuples. If no more rows are available, it returns an empty list. • cursor.fetchone() method returns a single record or None if no more rows are available.
  • 8. FETCHALL( ) To fetch all rows from a database table, you need to follow these simple steps: – • Create a database Connection from Python. • Define the SELECT query. Here you need to know the table and its column details. • Execute the SELECT query using the cursor.execute() method. • Get resultSet (all rows) from the cursor object using a cursor.fetchall(). • Iterate over the ResultSet using for loop and get column values of each row. • Close the Python database connection. • Catch any SQL exceptions that may come up during the process.
  • 9. FETCHALL( ) import sqlite3 def getAllRows(): try: connection = sqlite3.connect('SQLite_Python.db') cursor = connection.cursor() print("Connected to SQLite") sqlite_select_query = """SELECT * from database_developers""" cursor.execute(sqlite_select_query) records = cursor.fetchall()
  • 10. FETCHALL( ) print("Total rows are: ", len(records)) print("Printing each row") for row in records: print("Id: ", row[0]) print("Name: ", row[1]) print("Email: ", row[2]) print("Salary: ", row[3]) print("n") cursor.close() except sqlite3.Error as error: print("Failed to read data from table", error) finally: if connection: connection.close() print("The Sqlite connection is closed") getAllRows() //function call to get all row
  • 11. FETCHMANY() • Retrieve a few rows from a table. • Here size is the number of rows to be retrieved. • This method fetches the next set of rows of a query result and returns a list of tuples. If no more rows are available, it returns an empty list. • Cursor’s fetchmany() method returns the number of rows specified by size argument. Default value is 1. If the specified size is 100, then it returns 100 rows. • Let try to fetch 3 rows from table using a cursor.fetchmany(size) rows = cursor.fetchmany([size=cursor.arraysize])
  • 12. FETCHMANY( ) import sqlite3 def getlimitedRows(size): try: connection = sqlite3.connect('SQLite_Python.db') cursor = connection.cursor() print("Connected to database") sqlite_select_query = """SELECT * from database_developers""" cursor.execute(sqlite_select_query) records = cursor.fetchmany(size)
  • 13. FETCHMANY( ) print("Fetching Total ", size," rows") print("Printing each row") for row in records: print("Id: ", row[0]) print("Name: ", row[1]) print("Email: ", row[2]) print("Salary: ", row[3]) print("n") cursor.close() except sqlite3.Error as error: print("Failed to read data from table", error) finally: if (connection): connection.close() print("The Sqlite connection is closed") getlimitedRows(3) // function call to get 3 row
  • 14. FETCHONE() • Retrieve only single rows from a table. • It can return a none if no rows are available in the result set. • cursor.fetchone() increments the cursor position by one and return the next row. rows = cursor.fetchone( )
  • 15. FETCHONE( ) import sqlite3 def getSingleRows(): try: connection = sqlite3.connect('SQLite_Python.db') cursor = connection.cursor() print("Connected to database") sqlite_select_query = """SELECT * from database_developers""" cursor.execute(sqlite_select_query) print("Fetching single row")
  • 16. FETCHONE( ) record = cursor.fetchone() print(record) print("Fetching next row") record = cursor.fetchone() print(record) cursor.close() except sqlite3.Error as error: print("Failed to read data from table", error) finally: if connection: connection.close() print("The Sqlite connection is closed") getSingleRows() // get single row
  • 17. CRUD OPERATION USING EXECUTE( ) • Retrieve only single rows from a table. • It can return a none if no rows are available in the result set. • cursor.fetchone() increments the cursor position by one and return the next row. rows = cursor.fetchone( )
  • 18. STEPS TO CONNECT WITH SQLITE DATABASE • Import sqlite3 module. • Use the connect( ) method. • Use the cursor( ) method. • Use the execute( ) method. • Extract result using fetchall( ). • Close cursor and connection objects. • Catch database exception if any that may occur during connection process.
  • 19. CREATE SQLITE TABLE FROM PYTHON Steps for create a table in SQLite from Python: – • Connect to SQLite using a sqlite3.connect(). • Prepare a create table query. • Execute the query using a cursor.execute(query)
  • 20. CREATE SQLITE TABLE FROM PYTHON import sqlite3 try: sqliteConnection = sqlite3.connect('SQLite_Python.db') sqlite_create_table_query = '''CREATE TABLE SqliteDb_developers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email text NOT NULL UNIQUE, joining_date datetime, salary REAL NOT NULL);'''
  • 21. FETCHONE( ) cursor = sqliteConnection.cursor() print("Successfully Connected to SQLite") cursor.execute(sqlite_create_table_query) sqliteConnection.commit() print("SQLite table created") cursor.close() except sqlite3.Error as error: print("Error while creating a sqlite table", error) finally: if sqliteConnection: sqliteConnection.close() print("sqlite connection is closed")
  • 22. INSERT OPERATION • Connect to SQLite from Python • Define a SQL Insert query • Get Cursor Object from Connection • Execute the insert query using execute() method • Commit your changes • Get the number of rows affected • Verify result using the SQL SELECT query • Close the cursor object and database connection object
  • 23. INSERT import sqlite3 try: sqliteConnection = sqlite3.connect('SQLite_Python.db') cursor = sqliteConnection.cursor() print("Successfully Connected to SQLite") sqlite_insert_query = """INSERT INTO SqliteDb_developers (id, name, email, joining_date, salary) VALUES (1,'James','[email protected]','2019-03-17',8000)"""
  • 24. INSERT count = cursor.execute(sqlite_insert_query) sqliteConnection.commit() print("Record inserted successfully into SqliteDb_developers table ", cursor.rowcount) cursor.close() except sqlite3.Error as error: print("Failed to insert data into sqlite table", error) finally: if sqliteConnection: sqliteConnection.close() print("The SQLite connection is closed")
  • 25. INSERT OPERATION USING PYTHON VARIABLE • Sometimes we need to insert a Python variable value into a table’s column. • This value can be anything, including integer, string, float, and DateTime. • For example, in the registration form person enter his/her details. You can take those values in Python variables and insert them into the SQLite table. • We use a parameterized query to insert Python variables into the table. • Using a parameterized query, we can pass python variables as a query parameter in which placeholders (?)
  • 26. INSERT OPERATION USING PYTHON VARIABLE import sqlite3 def insertVaribleIntoTable(id, name, email, joinDate, salary): try: sqliteConnection = sqlite3.connect('SQLite_Python.db') cursor = sqliteConnection.cursor() print("Connected to SQLite") sqlite_insert_with_param = """INSERT INTO SqliteDb_developers (id, name, email, joining_date, salary) VALUES (?, ?, ?, ?, ?);"""
  • 27. INSERT OPERATION USING PYTHON VARIABLE data_tuple = (id, name, email, joinDate, salary) cursor.execute(sqlite_insert_with_param, data_tuple) sqliteConnection.commit() print("Python Variables inserted successfully into SqliteDb_developers table") cursor.close() except sqlite3.Error as error: print("Failed to insert Python variable into sqlite table", error)
  • 28. INSERT OPERATION USING PYTHON VARIABLE finally: if sqliteConnection: sqliteConnection.close() print("The SQLite connection is closed") insertVaribleIntoTable(2, 'Joe', '[email protected]', '2019-05-19', 9000) insertVaribleIntoTable(3, 'Ben', '[email protected]', '2019-02-23', 9500)
  • 29. INSERT MULTIPLE ROW • Sometimes we need to insert multiple rows into the table in a single insert query. • For example, You wanted to add all records from the CSV file into the SQLite table. • Instead of executing the INSERT query every time to add each record, you can perform a bulk insert operation in a single query using a cursor’s executemany() function. • The executemany() method takes two arguments SQL query and records to update.
  • 30. INSERT MULTIPLE ROW import sqlite3 def insertMultipleRecords(recordList): try: sqliteConnection = sqlite3.connect('SQLite_Python.db') cursor = sqliteConnection.cursor() print("Connected to SQLite") sqlite_insert_query = """INSERT INTO SqliteDb_developers (id, name, email, joining_date, salary) VALUES (?, ?, ?, ?, ?);"""
  • 31. INSERT MULTIPLE ROW cursor.executemany(sqlite_insert_query, recordList) sqliteConnection.commit() print("Total", cursor.rowcount, "Records inserted successfully into SqliteDb_developers table") sqliteConnection.commit() cursor.close() except sqlite3.Error as error: print("Failed to insert multiple records into sqlite table", error)
  • 32. INSERT MULTIPLE ROW finally: if sqliteConnection: sqliteConnection.close() print("The SQLite connection is closed") recordsToInsert = [(4, 'Jos', '[email protected]', '2019-01-14', 9500), (5, 'Chris', '[email protected]', '2019-05-15', 7600), (6, 'Jonny', '[email protected]', '2019-03-27', 8400)] insertMultipleRecords(recordsToInsert)
  • 33. UPDATE • Most of the time, we need to update a table with some runtime values. • For example, when users update their profile or any other details through a user interface, we need to update a table with those new values. • In such cases, It is always best practice to use a parameterized query. • The parameterized query uses placeholders (?) inside SQL statements that contain input from users. • It helps us to update runtime values and prevent SQL injection concerns.
  • 34. UPDATE import sqlite3 def updateSqliteTable(id, salary): try: sqliteConnection = sqlite3.connect('SQLite_Python.db') cursor = sqliteConnection.cursor() print("Connected to SQLite") sql_update_query = """Update SqliteDb_developers set salary = ? where id = ?""" data = (salary, id) cursor.execute(sql_update_query, data)
  • 35. UPDATE sqliteConnection.commit() print("Record Updated successfully") cursor.close() except sqlite3.Error as error: print("Failed to update sqlite table", error) finally: if sqliteConnection: sqliteConnection.close() print("The sqlite connection is closed") updateSqliteTable(3, 7500)
  • 36. UPDATE • We used two placeholders in the update query, one for the salary column and the other is for the id column. • Next, We prepared a data tuple by specifying two Python variables in sequential order. • Next, we passed the SQL update query and data tuple to the cursor.execute() method. Remember variables order in the tuple is sequential as per column placeholders order.
  • 37. UPDATE MULTIPLE ROW USING CURSOR • In the above example, we have used execute() method of cursor object to update a single record. But sometimes, we need to update multiple rows of the SQLite table. • For example, you want to increase the salary of developers by 20%. • Instead of executing the UPDATE query every time to update each record, you can perform bulk update operations in a single query using the cursor.executemany() method. • The executemany(query, seq_param) method accepts the following two parameters • SQL query • list of records to be updated.
  • 38. UPDATE MULTIPLE ROW USING CURSOR import sqlite3 def updateMultipleRecords(recordList): try: sqliteConnection = sqlite3.connect('SQLite_Python.db') cursor = sqliteConnection.cursor() print("Connected to SQLite") sqlite_update_query = """Update SqliteDb_developers set salary = ? where id = ?""" cursor.executemany(sqlite_update_query, recordList)
  • 39. UPDATE MULTIPLE ROW USING CURSOR sqliteConnection.commit() print("Total", cursor.rowcount, "Records updated successfully") sqliteConnection.commit() cursor.close() except sqlite3.Error as error: print("Failed to update multiple records of sqlite table", error)
  • 40. UPDATE MULTIPLE ROW USING CURSOR finally: if sqliteConnection: sqliteConnection.close() print("The SQLite connection is closed") records_to_update = [(9700, 4), (7800, 5), (8400, 6)] updateMultipleRecords(records_to_update)
  • 41. UPDATE MULTIPLE ROW USING CURSOR • We prepared the SQLite update query with two placeholders (“salary” and “Id” column ) and a list of records to update in tuple format. • Each element of a list is nothing but a tuple for each row. Each tuple contains two values, i.e., salary and id of a developer. • We passed SQLite update query and record list to executemany() as arguments. • To get to know the number of records updated, we used a cursor.rowcount function.
  • 42. DELETE • In order to delete data in the SQLite database from a Python program, you use the following steps: • First, establish a connection the SQLite database by creating a Connection object using the connect() function. • Second, to execute a DELETE statement, you need to create a Cursor object using the cursor() method of the Connection object. • Third, execute the DELETE statement using the execute() method of the Cursor object. In case you want to pass the arguments to the statement, you use a question mark (?) for each argument.
  • 43. EXAMPLE: #import the module. import sqlite3; #Use the connect( ) method for open or create a database con = sqlite3.connect("MyDB.db") print("Database Created") #Use the cursor( ) method using connection object cur = con.cursor() #create a table using execute() method createtbl = "create table test(ID int primary key, name text not null, total int not null)" con.execute(createtbl) print("Table Created")
  • 44. INSERT DATA #insert the data using execute() method insertquery = "insert into test values(1,'Nidhi',78);" con.execute(insertquery) print("data inserted", con.total_changes) #take input from user to insert data into table. id1 = input("Enter ID:") name = input("Enter Name:") total = input("Enter Total:") con.execute("insert into test values(?,?,?)", (id1,name,total)) print("data inserted", con.total_changes)
  • 45. INSERT MULTIPLE DATA #insert multiple data into table using executemany() method. records = [(3,'mnp',33), (4,'pqr',67),(6,'wxy',88)] con.executemany("insert into test values(?,?,?)", records) print("data inserted", con.total_changes)
  • 46. UPDATE DATA #update the data using execute() method updatequery = "update test set total=90 where id=1" con.execute(updatequery) print("data updated", con.total_changes) #take input from user to update data into table. idtoupadte = input("Enter id:") con.execute("update test set total=99 where id=?", idtoupadte) print("data updated", con.total_changes)
  • 47. UPDATE DATA #take multiple input from user to update data. updatename=input("enter update name:") updateid = input("enter id:") con.execute("update test set name=? where id=?",(updatename,updateid)) print("data updated", con.total_changes) #update multiple data into table using executemany() method. updatedata = [('priya',55,3), ('payal', 56,4), ('pritam',57,6)] con.executemany("update test set name=?, total=? where id=?",updatedata) print("data updated", con.total_changes)
  • 48. DELETE DATA #delete data from table using execute() method deletequery = "delete from test where id=1" con.execute(deletequery) print("data deleted", con.total_changes) #take input from user to delete data into table. deleteid = input("Enter id:") con.execute("delete from test where id=?",deleteid) print("data deleted", con.total_changes)
  • 49. SELECT DATA #select data from table usinf cursor() object and print the data using for loop. data = con.execute("SELECT * from test") for row in data: print ("ID = ", row[0]) print ("NAME = ", row[1]) print ("TOTAL = ", row[2])
  • 50. SELECT DATA- FETCHALL() #use the fetchall() method to get all data. cur.execute("select * from test") data1 = cur.fetchall() for row in data1: print ("ID = ", row[0]) print ("NAME = ", row[1]) print ("TOTAL = ", row[2])
  • 51. SELECT DATA- FETCHONE() AND FECTHMANY() #use fetchone() to get a single record cur.execute("select * from test") data2 =cur.fetchone() print(data2) #use fetchmany() method to get number of data record cur.execute("select * from test") data3 = cur.fetchmany(2) print(data3)
  • 52. EXAMPLE #print the list of tables of database print("List of tables:") cur.execute("SELECT * FROM sqlite_master WHERE type='table';") print(cur.fetchall()) #print the structure of a table. a= con.execute("PRAGMA table_info('test')") print("nTable structure of test:", a) for i in a: print(i)
  • 53. EXAMPLE #print the foreign keys in database rows=cur.execute('pragma foreign_keys') for r in rows: print (r) #use commit() method at last to save changes in your table con.commit() #use close() method to close the connection con.close()
  • 54. WHAT IS EXCEPTION? • An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error. • When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.
  • 55. HANDLING AN EXCEPTION? • If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
  • 56. HANDLING AN EXCEPTION? • The try block lets you test a block of code for errors. • The except block lets you handle the error. • The else block lets you execute code when there is no error. • The finally block lets you execute code, regardless of the result of the try- and except blocks. • When an error occurs, or exception as we call it, Python will normally stop and generate an error message. • These exceptions can be handled using the try statement:
  • 57. TRY EXCEPT • This statement will raise an error, because x is not defined: print(x) • If we write below statement then it print a error statement rather than throw a error. try: print(x) except: print("An exception occurred") Output: An exception occurred
  • 58. MANY EXCEPTION • You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error: try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong") Output: Variable x is not defined • Print one message if the try block raises a NameError and another for other errors:
  • 59. ELSE • You can use the else keyword to define a block of code to be executed if no errors were raised: try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong") Output: Hello Nothing went wrong
  • 60. FINALLY • The finally block, if specified, will be executed regardless if the try block raises an error or not. try: print(x) except: print("Something went wrong") finally: print("The 'try except' is finished") Output: Something went wrong The 'try except' is finished