0

Have tried with both (?) and (%s) but doesn't seem to be working. Where am I gong wrong?

def update(phone,name):
    conn = sqlite3.connect(db)
    print ("\nOpened database for updates successfully")

    sql = "UPDATE VARUN set PHONE = %s where NAME= %s "
    print (sql)
    conn.execute(sql,(phone,name))
    '''
    conn.execute("UPDATE VARUN set PHONE = (?) where NAME= (?) ",(phone,name));
    '''
    conn.commit()

----- calling function ----

contactlist[selection()]=[nameVar.get(), phoneVar.get()]
updt = (contactlist[selection()])
name = (updt[0])
phone = (updt[1])
print (name,phone)

try:
    update(name,phone)
except:
    tkinter.messagebox.showwarning("cannot be blank")
else:

    setList ()
    saveContact()
2
  • conn.execute('UPDATE VARUN set PHONE = ? where NAME = ?', (phone, name)) should work fine. Commented Jul 31, 2017 at 23:47
  • @poke Thanks for pointing that out. But it still doesn't update in the database. Can let know how can i debug this? All other operations like insert are working fine, so there is problem with the database. Commented Aug 1, 2017 at 0:51

1 Answer 1

2

First of all, you are using a bare except clause which prevents you from seeing what errors are thrown from the function. Remove it and see how it fails.

And, you need to have ? placeholders without the surrounding parenthesis:

def update(phone, name):
    conn = sqlite3.connect(db)
    cursor = conn.cursor()
    print ("\nOpened database for updates successfully")

    sql = "UPDATE VARUN set PHONE = ? where NAME= ?"
    cursor.execute(sql, (phone, name))
    conn.commit()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for pointing that out. I'm not getting any errors after changing it but it still doesn't update in the database. Can let know how can i debug this? All other operations like insert are working fine, so there is problem with the database.
@Varun well, make sure you are actually updating the same table in the same database as you are checking the results in. I've also noticed that the title says about mysql but there is sqlite3 connection code posted..thanks.
I cross checked and everything seems to be fine. But somehow it doesn't insert. Is there a way to print the query being executed? I tried converting it to string and print but it still prints as object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.