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)
importsqlite3# Connection with the DataBase# 'library.db'connection=sqlite3.connect("library.db")cursor=connection.cursor()# SQL piece of code Executed# SQL piece of code Executedcursor.executescript("""CREATE TABLE people(firstname,lastname,age);CREATE TABLE book(title,author,published);INSERT INTObook(title, author, published)VALUES ('Dan Clarke''s GFG Detective Agency','Sean Simpsons',1987);""")sql="""SELECT COUNT(*) FROM book;"""cursor.execute(sql)# The output in fetched and returned# as a List by fetchall()result=cursor.fetchall()print(result)sql="""SELECT * FROM book;"""cursor.execute(sql)result=cursor.fetchall()print(result)# Changes saved into databaseconnection.commit()# Connection closed(broken)# with DataBaseconnection.close()chevron_rightfilter_noneOutput:
[(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.
importsqlite3# Connection with the DataBase# 'library.db'connection=sqlite3.connect("library.db")cursor=connection.cursor()# SQL piece of code Executedcursor.execute("""CREATE TABLE book(title,author,published);""")List=[('A','B',2008), ('C','D',2008),('E','F',2010)]connection. executemany("""INSERT INTObook(title, author, published)VALUES (?, ?, ?)""",List)sql="""SELECT * FROM book;"""cursor.execute(sql)result=cursor.fetchall()forxinresult:print(x)# Changes saved into databaseconnection.commit()# Connection closed(broken)# with DataBaseconnection.close()chevron_rightfilter_noneOutput:
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 definedThe use of executemany(), can make the piece of code functional.
importsqlite3# Connection with the DataBase# 'library.db'connection=sqlite3.connect("library.db")cursor=connection.cursor()# SQL piece of code Executedcursor.execute("""CREATE TABLE book(title,author,published);""")List=[('A','B',2008), ('C','D',2008),('E','F',2010)]connection. executemany("""INSERT INTObook(title, author, published)VALUES (?, ?, ?)""",List)sql="""SELECT * FROM book;"""cursor.execute(sql)result=cursor.fetchall()forxinresult:print(x)# Changes saved into databaseconnection.commit()# Connection closed(broken)# with DataBaseconnection.close()chevron_rightfilter_noneOutput:
('A', 'B', 2008) ('C', 'D', 2008) ('E', 'F', 2010) -
Fetch Large Data
importsqlite3# Connection created with the# database using sqlite3.connect()connection=sqlite3.connect("company.db")cursor=connection.cursor()# Create Table command executedsql="""CREATE TABLE employee (ID INTEGER PRIMARY KEY,fname VARCHAR(20),lname VARCHAR(30),gender CHAR(1),dob DATE);"""cursor.execute(sql)# Single Tuple insertedsql="""INSERT INTO employeeVALUES (1007, "Will", "Olsen", "M", "24-SEP-1865");"""cursor.execute(sql)# Multiple Rows insertedList=[(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")# Multiple Rows fetched from# the Databaseforrowinconnection.execute('SELECT * FROM employee ORDER BY ID'):print(row)print("\nMethod-2\n")# Method-2 to fetch multiple# rowssql="""SELECT * FROM employee ORDER BY ID;"""cursor.execute(sql)result=cursor.fetchall()forxinresult:print(x)connection.commit()connection.close()chevron_rightfilter_noneOutput:
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.
Recommended Posts:
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Exception Handling in PL/SQL
- Youtube Data API for handling videos | Set-1
- Youtube Data API for handling videos | Set-2
- Youtube Data API for handling videos | Set-3
- Youtube Data API for handling videos | Set-4
- Youtube Data API for handling videos | Set-5
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python
- Error Handling in Python using Decorators
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between SQL and T-SQL
- SQL | Checking Existing Constraints on a Table using Data Dictionaries
- Manipulate R Data Frames Using SQL
- Handling missing keys in Python dictionaries
- Precision Handling in Python
- Python Exception Handling
- File Handling in Python
- Multiple Exception Handling in Python
- Python | Handling recursion limit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

