SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Python / SQLite Python: Updating Data

SQLite Python: Updating Data

Summary: in this tutorial, we will show you how to update data in the SQLite database from a Python program using the sqlite3 module.

To update data in a table from a Python program, you follow these steps:

  1. First, create a database connection to the SQLite database using the connect() function. Once the database connection created, you can access the database using the Connection object.
  2. Second, create a Cursor object by calling the cursor() method of the Connection object.
  3. Third, execute the UPDATE statement by calling the execute() method of the Cursor object.

In this example we will update the priority, begin date, and end date of a specific task in the tasks table.

To create a database connection, you use the following create_connection() function:

def create_connection(db_file): """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) return conn
Code language: SQL (Structured Query Language) (sql)

This update_task() function update a specific task:

def update_task(conn, task): """ update priority, begin_date, and end date of a task :param conn: :param task: :return: project id """ sql = ''' UPDATE tasks SET priority = ? , begin_date = ? , end_date = ? WHERE id = ?''' cur = conn.cursor() cur.execute(sql, task) conn.commit()
Code language: Python (python)

The following main() function creates a connection to the database located in C:\sqlite\db\pythonsqlite.db folder  and call the update_task() function to update a task with id 2:

def main(): database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection(database) with conn: update_task(conn, (2, '2015-01-04', '2015-01-06', 2)) if __name__ == '__main__': main()
Code language: Python (python)

Here is the full program:

import sqlite3 from sqlite3 import Error def create_connection(db_file): """ create a database connection to the SQLite database specified by the db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect(db_file) except Error as e: print(e) return conn def update_task(conn, task): """ update priority, begin_date, and end date of a task :param conn: :param task: :return: project id """ sql = ''' UPDATE tasks SET priority = ? , begin_date = ? , end_date = ? WHERE id = ?''' cur = conn.cursor() cur.execute(sql, task) conn.commit() def main(): database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection(database) with conn: update_task(conn, (2, '2015-01-04', '2015-01-06', 2)) if __name__ == '__main__': main()
Code language: Python (python)

After executing the program, you can connect to the database via sqlite3 command shell:

Use these command to format the output:

sqlite> .header on sqlite> .mode column
Code language: CSS (css)

And use the following statement to get the task with id 2:

SELECT * FROM tasks WHERE id = 2;
Code language: SQL (Structured Query Language) (sql)

As shown clearly from the screenshot, the task with id 2 has been updated successfully.

In this tutorial, you have learned how to update data in a table from a Python program.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Python: Inserting Data
Next SQLite Python: Deleting Data

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.