Python Tutorial

Step-by-Step Connecting SQLite Database in Python

How to Connect to SQLite Database in Python?

The Python code below demonstrates how to connect to an existing database. If the database does not already exist, it will be built before a database object is returned.

#!/usr/bin/python

import sqlite3

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

print "Opened database successfully";

To build a database in RAM, you can also specify database name as the special name:memory:. Let's now use the aforementioned command to create the database test.db in the current directory. 

You have the option to adjust your course based on your needs. Keep the above code in the sqlite.py file and run it like this: If the database is successfully built, the following message will appear.

$chmod +x sqlite.py

$./sqlite.py

Open database successfully

Did you find this article helpful?