Monday, September 23, 2024
Google search engine
HomeLanguagesHow to Execute a Script in SQLite using Python?

How to Execute a Script in SQLite using Python?

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


Output:

Example 2:

Python3




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


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments