: python SQqlite, Create Table, CRUD Operations
If you would like to become a Python backend programmer, you’ll need to learn how to connect Python to an SQLite database and execute standard CRUD tasks. By the end of this, the post you will have learned:
- How to Create a Python SQLite Connection
- How to Create an SQLite Database Table
- How to perform ou CRUD operations
- How to Connect Python with SQLite Database
- Step 1: First of all, we need to import the sqlite3 module in our python code.
import sqlite3
- Step 2: Using the connect () method connect your python to the SQLite database
# Connect to sqlite database
con = sqlite3.connect(‘studentDb.db’)
- Step 3: You have now connected to the SQLite database, using the cursor () to return your cursor object.
# Cursor object
cur = con.cursor()
- Step 4: You can now run any SQL query using the execute() method. For instance, let’s select data from the Studentsmarks table in the database.
# Execute SQL query
cur.execute(“Select * from Studentsmarks”)
- Step 5: lastly, close your database connection using the con.close() method.
# Close connection
con.close()