How to Connect SQLite With Python - Step By Step Guide


 

How to Connect SQLite With Python - Step By Step Guide

In this tutorial, we will be learning about SQLite database and how to connect it with python for the development of database applications. SQLite as the name says, it's a simple, single file database. There is no need of downloading and installing additional files. SQLite comes packed with most python installations. From version 2.5.x onward the SQLite module comes along as a default module.

Importing SQLite Module:

import sqlite3

Creating a database in SQLite is very simple as if we create a a directory or a file. Database in SQLite is created during the connection phase. Database will be stored as a file in the specified directory. 
Now, let's connect to our database:

Create a Database and make a connection:

import sqlite3
con=sqlite3.connect('mydata.db')

From the above code block, we can see that, the connect method is provided with a parameter 'mydata.db' . This is what our database name is. If the file is already created, then the connect method will try to connect with the database or will create a new database file as 'mydata.db'. 
Create a File and Save it as sqlconnect.py. Now, create a function to call everytime where we require database connectivity.

import sqlite3

def dbconnect():
    con=sqlite3.connect('mydata.db')
    return con


In the next tutorial we will be learning about creating tables and Inserting data.
Thank you.

Post a Comment

Previous Post Next Post