Connecting to SQLite Database
- To use SQLite, we must import sqlite3.
import sqlite3
- Then create a connection using connect() method and pass the name of the database you want to access if there is a file with that name, it will open that file. Otherwise, Python will create a file with the given name.
sqliteConnection = sqlite3.connect('gfg.db')
- After this, a cursor object is called to be capable to send commands to the SQL.
cursor = sqliteConnection.cursor()
Example: Connecting to SQLite3 database using Python
Python3
import sqlite3 # connecting to the database connection = sqlite3.connect( "gfg.db" ) # cursor crsr = connection.cursor() # print statement will execute if there # are no errors print ( "Connected to the database" ) # close the connection connection.close() |
Output:
Connected to the database
SQL using Python
In this article, integrating SQLite3 with Python is discussed. Here we will discuss all the CRUD operations on the SQLite3 database using Python. CRUD contains four major operations –
Note: This needs a basic understanding of SQL.
Here, we are going to connect SQLite with Python. Python has a native library for SQLite3 called sqlite3. Let us explain how it works.