Python SQLite Update

by Atakan

Hello, Let’s look at an example for another frequently used SQL operation update.

of course we will import sqlite3

import sqlite3

Then connection object and the cursor object.

conn = sqlite3.connect("contact_list.db")
cursor = conn.cursor()

and our method to operate

def update_my_contact(id,name, number, city, alias):
    query = "UPDATE contact_table SET name = ? , number = ? , city = ? , alias= ?  WHERE id=?"
    params = (name, number, city, alias, id)
    cursor.execute(query, params)
    conn.commit()

full code of

update.py
import sqlite3

conn = None
cursor = None


def init_connections():
    global conn
    global cursor
    conn = sqlite3.connect("contact_list.db")
    cursor = conn.cursor()

def close_connecton():
    cursor.close()
    conn.close()


def update_my_contact(id,name, number, city, alias):
    query = "UPDATE contact_table SET name = ? , number = ? , city = ? , alias= ?  WHERE id=?"
    params = (name, number, city, alias, id)
    cursor.execute(query, params)
    conn.commit()

init_connections()
update_my_contact(2,"changed Name","changed Number","changed city","changed alias")
close_connecton()

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. OK Read More