sqliteHow can I use Python to update a SQLite database?
Using Python to update a SQLite database is a simple process. The following example code block demonstrates how to update a row in an existing table:
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Update a row in the table
c.execute("UPDATE example_table SET column1 = 'value1' WHERE column2 = 'value2'")
# Commit the changes
conn.commit()
# Close the connection
conn.close()
The code above will update a row in the table example_table
where column2
is equal to value2
and set column1
to value1
.
The code consists of the following parts:
import sqlite3
- Imports thesqlite3
module, which provides an interface to the SQLite database.conn = sqlite3.connect('example.db')
- Connects to the SQLite database fileexample.db
.c = conn.cursor()
- Creates a cursor object, which is used to execute SQL queries.c.execute("UPDATE example_table SET column1 = 'value1' WHERE column2 = 'value2'")
- Executes an SQL query to update a row in the tableexample_table
.conn.commit()
- Commits the changes to the database.conn.close()
- Closes the connection to the database.
For more information, see the following 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 can I query a SQLite database for records from yesterday's date?
- How do I use UUIDs in SQLite?
- How do I use a SQLite GUID?
- How can I use SQLite to query for records between two specific dates?
- How do I use SQLite to retrieve data from a specific year?
- How can I use SQLite with Zabbix?
- How do I install and use SQLite on Ubuntu?
- How do I import data from a SQLite zip file?
See more codes...