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 can SQLite and ZFS be used together for software development?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use SQLite to retrieve data from a specific year?
- How do I generate XML output from a SQLite database?
- How can I use an upsert statement to update data in a SQLite database?
- How do I use SQLite to zip a file?
- How to configure SQLite with XAMPP on Windows?
- How do I extract the year from a datetime value in SQLite?
- How can I get the year from a date in SQLite?
- How do I use the SQLite VARCHAR data type?
See more codes...