working-with-sqlite3-in-python

This tutorial mainly focuses on

  • Creating database in SQLite3 along with Python
  • Creating table in SQLite3 along with Python
  • Making database connection in SQLite3 with Python

python-video-tutorials

1). Creating database: To create database follow below given steps:

  1. Open DB Browser (SQLite).
  2. Click on create database
  3. Choose location where you want to save database and give the name to database.
  4. Now click save. Hence our database is created. click here to understand in detail.

2). Creating table: To create table in database follow below given steps:

  1. Open DB Browser (SQLite).
  2. Click on open database.
  3. Locate your database file and choose the file that we have created.
  4. Click open and you will see the below given interface:
    creating-table-sqlite
  5. Now we will click on create table. after clicking in create table below interface will be open:table-create-sqlite
  6. Now we will provide table name (i provided “user” as table name). And create columns by  clicking on add fields. Click here to understand about constraints.
  7. Click OK and now our table is successfully created.
    You will see this interface after table creation:table-in-sqlite

3). Database Connection SQLite3: As we successfully created our database and table. Now we will make connection to our database using python script or code.

In python we have a library named as sqlite3 which contains all the modules that we require to perform operation on our databases. So we will import this module to make our work easier. Below is the python program to connect to database in SQLite3:

Python Program for Database Connectivity to SQLite3

import sqlite3

con = sqlite3.connect("abc.db")
if(con):
    print("Database connected successfully")

Output:

# Database connected successfully

Hence we have successfully connected to our database.