sqliteHow can I use SQLite with Python?
SQLite is a lightweight database that can be used with Python. To use SQLite with Python, you need to import the sqlite3 module.
import sqlite3
You can then create a connection to an SQLite database file, or you can create a new database file.
# Create a connection to a database file
conn = sqlite3.connect('example.db')
# Create a new database file
conn = sqlite3.connect('new_example.db')
Once you have a connection, you can create a cursor object and call its execute() method to run SQL commands.
# Create a cursor object
c = conn.cursor()
# Execute a SQL command
c.execute("CREATE TABLE IF NOT EXISTS students (name TEXT, grade INTEGER);")
You can also use the cursor's execute() method to insert, update, and delete data.
# Insert data
c.execute("INSERT INTO students VALUES ('John', 90);")
# Update data
c.execute("UPDATE students SET grade = 95 WHERE name = 'John';")
# Delete data
c.execute("DELETE FROM students WHERE name = 'John';")
Finally, you should commit the changes and close the connection.
# Commit changes
conn.commit()
# Close connection
conn.close()
Helpful links
More of Sqlite
- How do I generate a row number for each record in a SQLite database?
- How do I use SQLite with Zephyr?
- How can SQLite and ZFS be used together for software development?
- How can I use Python to update a SQLite database?
- How can I use an upsert statement to update data in a SQLite database?
- How do I show the databases in SQLite?
- How do I use the SQLite SELECT statement?
- How do I extract the year from a datetime value in SQLite?
- How can I use SQLite to query for records between two specific dates?
- How can I use SQLite with Xamarin?
See more codes...