Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Execute a Script in SQLite using Python?

  • Last Updated : 09 May, 2021

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 module
import sqlite3
  
# create con object to connect 
# the database geeks_db.db
con = sqlite3.connect("geeks_db.db")
  
# create the cursor object
cur = con.cursor()
  
# execute the script by creating the 
# table named geeks_demo and insert the data
cur.executescript("""
    create table geeks_demo(
        geek_id,
        geek_name
    );
   insert into geeks_demo values ( '7058', 'sravan kumar' );
   insert into geeks_demo values ( '7059', 'Jyothika' );
   insert into geeks_demo values ( '7072', 'Harsha' );
   insert into geeks_demo values ( '7075', 'Deepika' );
      
    """)
  
# display the data in the table by 
# executing the cursor object
cur.execute("SELECT * from geeks_demo")
  
# fetch all the data
print(cur.fetchall())

Output:

Example 2:

Python3




# import sqlite3 module
import sqlite3
  
# create con object to connect 
# the database geeks_db.db
con = sqlite3.connect("geeks_db.db")
  
# create the cursor object
cur = con.cursor()
  
# execute the script by creating the table
# named geeks1 and insert the data
cur.executescript("""
    create table geeks1(
        geek_id,
        geek_name,
        address
    );
   insert into geeks1 values ( '7058', 'sravan kumar','hyd' );
   insert into geeks1 values ( '7059', 'Jyothika' ,'ponnur' );
   insert into geeks1 values ( '7072', 'Harsha','chebrolu'  );
   insert into geeks1 values ( '7075', 'Deepika','tenali'  );
      
    """)
  
# display the data in the table by 
# executing the cursor object
cur.execute("SELECT * from geeks1")
  
# fetch all the data
print(cur.fetchall())

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :