Knowledge Base
MilesWeb / How-Tos

How can I connect to SQLite using Python?

Approx. read time : 2 min

This article describes steps to connect to SQLite using Python.

Getting connect to SQLite using Python :

The sqlite3 module is a part of Python’s standard library which includes all the functionalities that you need to access and work with SQLite databases. To work with SQLite in Python, you don’t have to install any vendor-specific modules.

The following code demonstrates how you can connect to a SQLite database using the sqlite3 module and how you can do some basic manipulations :

In this example shown, we have first created a Connection object that will open SQLite database. In case the test.db file exists already, Python will open that file. Otherwise, Python will create a new database in the test.db.

Once we get a Connection object associated with the database, next we can create a Cursor object. The Cursor object allows us to run the execute() method, which then allows us to run raw SQL statements (like CREATE TABLE, SELECT, etc).

At-last, we can call the close() method in-order to close the connection to the database.

For more information about sqlite3 method, visit this :

https://docs.python.org/2/library/sqlite3.html

Also Read :

1) How can I connect to SQLite using PHP?

2) SQLite vs MySQL – How Different They Are?

Sonam Wagh
With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.
Need help? We’re always here for you.