0% found this document useful (0 votes)
22 views

Introduction To Python Sqlite

Uploaded by

dbullagaddimat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Introduction To Python Sqlite

Uploaded by

dbullagaddimat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction To Python SQLite

Python SQLite

 Python sqlite3 is an excellent module with which we can perform all


possible database operations with in memory.

 The sqlite3 module in Python provides a simple and convenient way


to interact with SQLite databases.

SQLite is a lightweight, serverless, self-contained database engine


that is often used for embedded systems and small to medium-sized
applications.
Advantages of using Python SQLite :

Lightweight and fast


No need of separate server
Cross platform
Reliable and durable
TheSQLite3module in python:

 Import the sqlite3 module:

importsqlite3

 Connect to a sqLite Database:

conn = sqlite3.connect('example.db')

 Create a Cursor Object:

cursor=conn.cursor()
Execute SQL Commands:

Create Tables:
cursor.execute('''CREATE TABLE IF NOT EXISTS
users( id INTEGER PRIMARY KEY,
nameTEXTNOTNULL,
age INTEGER
)''')

Insert Data into Tables:


cursor.execute("INSERT INTO
users(name,age)VALUES(?, ?)", ('JohnDoe',25))

Commit Changes:
conn.commit()
Query Data:
cursor.execute("SELECT*FROM users")
rows = cursor.fetchall()
for row in rows:
print(row)

 Update and Delete data:


#Updatedata
cursor.execute("UPDATE users SET
age=? WHERE name=?",
(30,'JohnDoe'))

#Deletedata
cursor.execute("DELETE FROM users
WHERE name=?", ('JohnDoe',))

Close the connection:


Conn.close()
Operations on Tables
Here's an example that demonstrates basic operations on an SQLite table:

Importsqlite3

conn= sqlite3.connect(‘001k.db')

cursor=conn.cursor()

cursor.execute('''CREATE TABLEIF NOT EXISTS users(


id INTEGER PRIMARY KEY,
nameTEXT NOTNULL,
age INTEGER
)''')

conn.commit()
 cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)",
(‘Namrata', 25))
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)",
(‘Dhanashree', 30))
cursor.execute("INSERT INTO users(name,age) VALUES(?,?)",
(‘Bhavani,22))

 conn.commit()

 cursor.execute("SELECT * FROM users")


rows = cursor.fetchall()
print("Records in'users'table:")
for row in rows:
print(row)
 cursor.execute("UPDATE users SET age=? WHERE name=?", (26,‘Namrata'))

 conn.commit()

 cursor.execute("SELECT*FROM users") rows = cursor.fetchall()


print("\nRecords in'users'table after update:") for row in rows:
print(row)

 cursor.execute("DELETE FROM users WHERE name=?",(‘Bhavani,))

 conn.commit()
 cursor.execute("SELECT*FROM users")
rows = cursor.fetchall()
print("\nRecords in'users‘ table after delete:")
for row in rows:
print(row)

 conn.close()
OUTPUT:
Records in 'users‘ table:
(1, ‘Namrata', 25)
(2, ‘Dhanashree', 30)
(3, ‘Bhavani',22)
Records in 'users‘ table after update:
(1, ‘Namrata', 26)
(2, ‘Dhanashree', 30)
(3, ‘Bhavani',22)
Records in ‘users‘ table after delete
(1, ‘Namrata', 26)
(2, ‘Dhanashree', 30)
Thank You

You might also like