SQLite is a file-based, lightweight database system that is widely used in the industry. It is popular for its easiness and simplicity, hence it serves as a good choice for tiny to medium-sized applications. This article will explore how to use SQLite with Python, covering the basics of setting up a database, performing CRUD operations and working with data effectively.
Getting Started with SQLite and Python
If you want to start using SQLite in Python, first make sure that you have installed SQLite library. You can accomplish this by running following command:
pip install pysqlite3
Once the library has been installed on your machine, you can include sqlite3 module into your python code so as to start utilizing it.
import sqlite3
Connecting to a SQLite Database
Following importation of sqlite3 module, you are able to connect to an instance of a SQLite database via this code snippet:
# Connect to the database (will create a new database if it doesn't exist) conn = sqlite3.connect('example.db')
This particular line creates an instance of connection object pointing to example.db. In cases where no such file exists on disk, then it will be created by sqlite automatically.
Creating a Table
When establishing connectivity with the database one may need to go ahead and create tables within it. Below is an example where we create simple table which stores details about users.
# Create a cursor object c = conn.cursor() # Create a table c.execute(''' CREATE TABLE users ( id INTEGER PRIMARY KEY, username TEXT NOT NULL, email TEXT NOT NULL ) ''') # Save (commit) the changes conn.commit()
Inserting Data
You can insert information into table using INSERT statement. For instance below there is inserting new users table:
# Insert a new user c.execute(''' INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com') ''') # Save (commit) the changes conn.commit()
Data Retrieval
using the SELECT statement, you can retrieve data from the database. Below is an example of querying all users from the users table;
# Query the database c.execute('SELECT * FROM users') # Fetch and print the results for row in c: print(row)
Updating Data
The UPDATE statement is used to update existing data in your table. Here's an example of updating a user's email address.
# Update a user's email c.execute(''' UPDATE users SET email = 'updated_email@example.com' WHERE username = 'john_doe' ''') # Save (commit) the changes conn.commit()
Deleting Data
DELETE statement is used for removing data from a table. An example of deleting a user from the users table:
# Delete a user c.execute(''' DELETE FROM users WHERE username = 'john_doe' ''') # Save (commit) the changes conn.commit()
Conclusion
Python developers are free to use SQLite because it goes together with this programming language as one database system that is very flexible and useful. It acts as an easy-to-use data manager for small-to-middle-sized applications and attracts developers by its simplicity in design. After going through this article with its examples and best practices, anyone using Python should find it simple using SQLite for their project data storage and manipulation tasks.