How to use SQLite with Python

Published on Aug. 22, 2023, 12:15 p.m.

To use SQLite with Python, you can use the sqlite3 module. Here’s an example:

import sqlite3

# Create a connection to the database
conn = sqlite3.connect('example.db')

# Get a cursor object
cur = conn.cursor()

# Create a table
cur.execute("CREATE TABLE my_table (id INTEGER PRIMARY KEY, name TEXT)")

# Insert some data
cur.execute("INSERT INTO my_table (name) VALUES ('Alice')")
cur.execute("INSERT INTO my_table (name) VALUES ('Bob')")

# Commit the changes
conn.commit()

# Query the data
cur.execute("SELECT id, name FROM my_table")
rows = cur.fetchall()
for row in rows:
    print(row)

# Close the connection
conn.close()

In this example, we first create a connection to the database using sqlite3.connect(). We then get a cursor object with conn.cursor(), and use it to execute SQL statements like creating a table, inserting data, and querying data.

After making changes, we commit them to the database with conn.commit(). We then query the data with cur.execute() and cur.fetchall(), and print out the results.

Finally, we close the connection with conn.close().

To read SQLite data in pandas using Python

To read SQLite data in pandas using Python, you can use the read_sql_query() function from the pandas library. Here is an example code snippet to illustrate how to do it:

import pandas as pd
import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect('example.db')

# Query the database and store the results in a pandas dataframe
df = pd.read_sql_query('SELECT * FROM mytable', conn)

# Close the database connection
conn.close()

# Print the first few rows of the dataframe
print(df.head())

In this example, we first connect to the SQLite database using the connect() function from the sqlite3 module. Then we use the read_sql_query() function from pandas to query the database and store the results in a pandas dataframe. Finally, we close the database connection and print the first few rows of the dataframe using the head() function. Note that you would need to replace ‘example.db’ with the name of your SQLite database file, and ‘mytable’ with the name of your table in the database.

SQLite

SQLite is a powerful database management system that can be used for a wide range of applications. Here are some of the amazing use cases of SQLite:

  1. Mobile App Development: SQLite is widely used in mobile app development, especially on iOS and Android platforms. Its small size, high performance and cross-platform portability make it ideal for use in mobile apps.
  2. IoT and Embedded Devices: SQLite’s lightweight nature and low resource requirements make it a popular choice for use in IoT and embedded devices. It can run on devices with limited memory and processing power, making it the perfect choice for resource-constrained environments.
  3. Testing and Prototyping: SQLite is commonly used in testing and prototyping of applications because it is easy to setup, fast and does not require a separate server process.
  4. Data Storage and Sharing: SQLite can store and share data in a wide variety of applications, including desktop applications, web applications, and serverless applications. Its ability to store data in a single file makes it easy to transfer data between computers.

Overall, SQLite is a versatile and powerful database management system that can be used for a wide range of applications.

Tags: