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 thesqlite3module, 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 do I call sqlitepcl.raw.setprovider() when using SQLite?
- How can I use an upsert statement to update data in a SQLite database?
- How do I generate XML output from a SQLite database?
- How do I use the SQLite zfill function?
- How do I use SQLite to retrieve data from a specific year?
- How to configure SQLite with XAMPP on Windows?
- How can I use SQLite with Zabbix?
- How do I extract the year from a datetime value in SQLite?
See more codes...