Python SQLite Database Connection
Python SQLite Database Connection
This tutorial will demonstrate connecting to an SQLite database using Python’s DB API. Connecting to an SQLite database in Python involves using the appropriate module. We can import the module using the following statement:
import sqlite3
SQLite Database Connection
To open an SQLite database connection, use the following API call in the Python program:
connection = sqlite3.connect (database [,timeout, other arguments])
This API opens a connection to the SQLite database file. If the given database name does not exist, this call will create the database in the current directory. You can specify the filename with the required path to create a database anywhere except in the current directory.
If a database is accessed simultaneously by multiple connections, and one of the processes modifies the database, then the SQLite database is locked until that transaction is committed. The timeout parameter determines how long the connection should wait before the lock disappears before raising an exception. By default, the timeout period is set to 5 seconds.
Example
# Open SQLite DB Connection
connection = sqlite3.connect(‘tdocs_database.db’)
In memory database
You can use “:memory:” to open a database connection to a database that resides in RAM instead of on disk. If the database is opened successfully, it returns a connection object.
connection = sqlite3.connect(“:memory:”)
—
Python Tutorials
Python Tutorial on this website can be found at:
https://www.testingdocs.com/python-tutorials/
More information on Python is available at the official website: