snaktastic
snaktastic

Reputation: 43

Insert data into specific Sqlite3 database row?

self.cursor.execute('INSERT INTO User where User_name=(?) (user_name,user_password,user_wins,user_loses) VALUES(?,?,?,?)',(a,a,b,c,d))
self.connect.commit()

Logically i would have thought this would work but im not sure why it hasnt? The first '?' in the sql statement is the same as 'a'. I get this error:

sqlite3.OperationalError: near "where": syntax error

So im wondering if it is actually possible to write to an already written row in a SQL database?

Upvotes: 0

Views: 54

Answers (1)

khelwood
khelwood

Reputation: 59111

INSERT means add a completely new row. If you want to alter an existing row, you need UPDATE.

For instance

UPDATE User
SET user_password=?,
    user_wins=?,
    user_loses=?
WHERE user_name=?

passing the user name as the last variable.

So something like:

self.cursor.execute(
    'UPDATE User SET user_password=?, user_wins=?, user_loses=? WHERE user_name=?',
    (b,c,d,a)
)

Upvotes: 3

Related Questions