9951 explained code solutions for 126 technologies


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:

  1. import sqlite3 - Imports the sqlite3 module, which provides an interface to the SQLite database.
  2. conn = sqlite3.connect('example.db') - Connects to the SQLite database file example.db.
  3. c = conn.cursor() - Creates a cursor object, which is used to execute SQL queries.
  4. c.execute("UPDATE example_table SET column1 = 'value1' WHERE column2 = 'value2'") - Executes an SQL query to update a row in the table example_table.
  5. conn.commit() - Commits the changes to the database.
  6. conn.close() - Closes the connection to the database.

For more information, see the following links:

Edit this code on GitHub