Getting Started with SQLite Databases
Learn the basics of SQLite databases and how to interact with them using Python.
Introduction to SQLite
SQLite is a self-contained, file-based database system that allows you to store and manage data in a structured way. To get started with SQLite, you'll need to install the sqlite3 module in Python. You can do this by running pip install pysqlite3 in your terminal.
Creating a Database
To create a new SQLite database, you can use the following code:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
This will create a new database file called example.db in the current working directory.
Creating a Table
Once you have a database, you can create a table to store data. Here's an example of how to create a table:
cursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
This will create a new table called users with three columns: id, name, and email.
Inserting Data
To insert data into the table, you can use the INSERT INTO statement. Here's an example:
cursor.execute("""INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')""")
conn.commit()
This will insert a new row into the users table with the name John Doe and email john@example.com.