0

I am trying to update values in my sqlite database using the below function

def updateNameById(self, id, val):
    c.execute("UPDATE {0} SET name = {1} WHERE rowid = {2}".format(table,val,id))

I also tried to write this function in different variations like

    c.execute("UPDATE "+table+" SET name = "+val+" WHERE rowid = "+id)
    #same error

I also tried

  c.execute("UPDATE ? table SET name = ? WHERE rowid = ?,(table,val,id))
  #this one gives syntax error near ?

when called from with code

print(obj.updateNameById(1,'Aquib'))

It gives sqlite3.OperationalError: no such column: Aquib

I also tried to put the id parameter as string and double single quotation stuff but all result in the same error after trying many combinations and looking up answers all over I finally posted this question

2 Answers 2

1

data base name : data.db, table : table. try this , it worked well for me.

def updateNameById(id, val):
    connection = sqlite3.connect('data.db')
    connection.row_factory = sqlite3.Row
    cursor = connection.cursor()    
    cursor.execute('UPDATE table SET name=? WHERE id=?',(val,id,))
    connection.commit()
Sign up to request clarification or add additional context in comments.

Comments

0
conn = sqlite3.connect("dbName")
cursor = conn.cursor()
def updateNameById(self, id, val):
    cursor.execute('UPDATE table SET name=? WHERE id=?',(val,id,))
    conn.commit()

Comments

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.