In this article, we are going to see how to execute a script in SQLite using Python. Here we are executing create table and insert records into table scripts through Python. In Python, the sqlite3 module supports SQLite database for storing the data in the database.
Approach
Step 1: First we need to import the sqlite3 module in Python.
import sqlite3
Step 2: Connect to the database by creating the database. We can connect to the database by simply create a database named geeks_db.db or we can simply create a database in our memory by using :memory:
Database creation by name
connection_object = sqlite3.connect(“database_name.db”)
Database creation in Memory:
connection_object = sqlite3.connect:memory:)
Step 3: Create the cursor object after making the database connection.
cursor_object = connection_object.cursor()
Step 4: Write the SQL query that can be executable.
cursor_object.executescript(“script”)
Step 5: Execute the cursor object
cursor_object(“sql statement”)
Step 6: Get the data inside the table from the database.
cursor_object.fetchall()
Example 1:
Python3
import sqlite3
con = sqlite3.connect( "geeks_db.db" )
cur = con.cursor()
cur.executescript(
)
cur.execute( "SELECT * from geeks_demo" )
print (cur.fetchall())
|
Output:

Example 2:
Python3
import sqlite3
con = sqlite3.connect( "geeks_db.db" )
cur = con.cursor()
cur.executescript(
)
cur.execute( "SELECT * from geeks1" )
print (cur.fetchall())
|
Output:
