Skip to main content

Command Palette

Search for a command to run...

Python SQLite Tutorial

Updated
โ€ข2 min read
Python SQLite Tutorial
N

I am a Tech Enthusiast having 13+ years of experience in ๐ˆ๐“ as a ๐‚๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐š๐ง๐ญ, ๐‚๐จ๐ซ๐ฉ๐จ๐ซ๐š๐ญ๐ž ๐“๐ซ๐š๐ข๐ง๐ž๐ซ, ๐Œ๐ž๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐“๐ž๐ฌ๐ญ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ƒ๐š๐ญ๐š ๐’๐œ๐ข๐ž๐ง๐œ๐ž. I have ๐’•๐’“๐’‚๐’Š๐’๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 10,000+ ๐‘ฐ๐‘ป ๐‘ท๐’“๐’๐’‡๐’†๐’”๐’”๐’Š๐’๐’๐’‚๐’๐’” and ๐’„๐’๐’๐’…๐’–๐’„๐’•๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 500+ ๐’•๐’“๐’‚๐’Š๐’๐’Š๐’๐’ˆ ๐’”๐’†๐’”๐’”๐’Š๐’๐’๐’” in the areas of ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ, ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐‚๐ฅ๐จ๐ฎ๐, ๐ƒ๐š๐ญ๐š ๐€๐ง๐š๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐ƒ๐š๐ญ๐š ๐•๐ข๐ฌ๐ฎ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž ๐š๐ง๐ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐  ๐›๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐š๐ซ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ, ๐ซ๐ž๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  new subjects.

SQLite is a relational database management system based on the SQL language but optimized for use in small environments such as mobile phones or small applications. It is self-contained, serverless, zero-configuration and transactional.

Creating Database

import sqlite3

db = sqlite3.connect('employees.db') # Open a database File
print('Database opened')
db.close()
print('Database Closed')

Create Table

import sqlite3

db = sqlite3.connect('employees.db') # Open a database File
query = '''
CREATE TABLE IF NOT EXISTS employee(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    DIVISION TEXT NOT NULL,
    STARS INT NOT NULL) 
'''
db.execute(query)
print('Database opened')
db.close()
print('Database Closed')

Insert Records

import sqlite3
print('Database opened')
db = sqlite3.connect('employees.db') # Open a database File
query = '''
INSERT INTO employee_records(ID,NAME,DIVISION,STARS) VALUES(1,'Ravi','Architect',4)
'''
db.execute(query)
db.commit()
print('REcord inserted')
db.close()
print('Database Closed')

Using variables to insert data

import sqlite3
print('Database opened')
db = sqlite3.connect('employees.db') # Open a database File

def insert_record(id, name, division, stars):
  query = ''' INSERT INTO employee_records(?,?,?,?)  VALUES(id, name, division,stars)
'''
  db.execute(query )
  db.commit()
  print('Record inserted')

insert_record(1,'Ravi','Architect',4)
insert_record(2,'Rahul','Architect',4)

db.close()
print('Database Closed')

Read Data

import sqlite3
print('Database opened')
db = sqlite3.connect('employees.db') # Open a database File

def read_records(id, name, division, stars):
  query = 'select * from employee'
  records = db.execute(query )
  for record in records:
    print(record)
  print('Record inserted')

read_records()
db.close()
print('Database Closed')

Update Records

import sqlite3
print('Database opened')
db = sqlite3.connect('employees.db') # Open a database File

def update_record(id,updated_stars):
  query = f"UPDATE employee set STARS={updated_stars} where id={id}"
  records = db.execute(query )
  db.commit()

update_record(1,5)
db.close()
print('Database Closed')

Delete Record

import sqlite3
print('Database opened')
db = sqlite3.connect('employees.db') # Open a database File

def delete_record(id):
  query = f"DELETE FROM employee WHERE id={id}"
  records = db.execute(query )
  db.commit()

delete_record(2)
db.close()
print('Database Closed')

Programming

Part 1 of 1

More from this blog

Naveen P.N's Tech Blog

94 posts