It is recommended to go through SQL using Python | Set 1 and SQL using Python and SQLite | Set 2
In the previous articles the records of the database were limited to small size and single tuple. This article will explain how to write & fetch large data from the database using module SQLite3 covering all exceptions.
A simple way is to execute the query and use fetchall(). This has been already discussed in SET 1.
-
executescript()
This is a convenience method for executing multiple SQL statements at once. It executes the SQL script it gets as a parameter.
Syntax:sqlite3.connect.executescript(script)
import sqlite3
connection = sqlite3.connect( "library.db" )
cursor = connection.cursor()
cursor.executescript(
)
sql =
cursor.execute(sql)
result = cursor.fetchall()
print (result)
sql =
cursor.execute(sql)
result = cursor.fetchall()
print (result)
connection.commit()
connection.close()
|
Output:
[(1,)]
[("Dan Clarke's GFG Detective Agency", 'Sean Simpsons', 1987)]
Note: This piece of code may not work on online interpreters, due to permission privileges to create/write database.
-
executemany()
It is often the case when, large amount of data has to be inserted into database from Data Files(for simpler case take Lists, arrays). It would be simple to iterate the code many a times than write every time, each line into database. But the use of loop would not be suitable in this case, the below example shows why. Syntax and use of executemany() is explained below and how it can be used like a loop.
import sqlite3
connection = sqlite3.connect( "library.db" )
cursor = connection.cursor()
cursor.execute(
)
List = [( 'A' , 'B' , 2008 ), ( 'C' , 'D' , 2008 ),
( 'E' , 'F' , 2010 )]
connection. executemany(
, List )
sql =
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print (x)
connection.commit()
connection.close()
|
Output:
Traceback (most recent call last):
File "C:/Users/GFG/Desktop/SQLITE3.py", line 16, in
List[2][3] =[['A', 'B', 2008], ['C', 'D', 2008], ['E', 'F', 2010]]
NameError: name 'List' is not defined
The use of executemany(), can make the piece of code functional.
import sqlite3
connection = sqlite3.connect( "library.db" )
cursor = connection.cursor()
cursor.execute(
)
List = [( 'A' , 'B' , 2008 ), ( 'C' , 'D' , 2008 ),
( 'E' , 'F' , 2010 )]
connection. executemany(
, List )
sql =
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print (x)
connection.commit()
connection.close()
|
Output:
('A', 'B', 2008)
('C', 'D', 2008)
('E', 'F', 2010)
-
Fetch Large Data
import sqlite3
connection = sqlite3.connect( "company.db" )
cursor = connection.cursor()
sql =
cursor.execute(sql)
sql =
cursor.execute(sql)
List = [( 1008 , 'Rkb' , 'Boss' , 'M' , "27-NOV-1864" ),
( 1098 , 'Sak' , 'Rose' , 'F' , "27-DEC-1864" ),
( 1908 , 'Royal' , 'Bassen' , "F" , "17-NOV-1894" )]
connection. executemany(
"INSERT INTO employee VALUES (?, ?, ?, ?, ?)" , List )
print ( "Method-1\n" )
for row in connection.execute( 'SELECT * FROM employee ORDER BY ID' ):
print (row)
print ( "\nMethod-2\n" )
sql =
cursor.execute(sql)
result = cursor.fetchall()
for x in result:
print (x)
connection.commit()
connection.close()
|
Output:
Method-1
(1007, 'Will', 'Olsen', 'M', '24-SEP-1865')
(1008, 'Rkb', 'Boss', 'M', '27-NOV-1864')
(1098, 'Sak', 'Rose', 'F', '27-DEC-1864')
(1908, 'Royal', 'Bassen', 'F', '17-NOV-1894')
Method-2
(1007, 'Will', 'Olsen', 'M', '24-SEP-1865')
(1008, 'Rkb', 'Boss', 'M', '27-NOV-1864')
(1098, 'Sak', 'Rose', 'F', '27-DEC-1864')
(1908, 'Royal', 'Bassen', 'F', '17-NOV-1894')
Note: This piece of code may not work on online interpreters, due to permission privileges to create/write database.
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our
Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our
Complete Interview Preparation course.
Last Updated :
24 Nov, 2020
Like Article
Save Article