rskw00
rskw00

Reputation: 61

How to update data into a specific column in a sqlite database?

I have a table containing the name, score 1, score 2, and score 3 of a player. There are multiple data entries and I would like to know how to add the player's score to their correct column, i.e.:

Name, Score 1, Score 2, Score 3
John,      12,    ____,    ____

How would I specify what column to insert the player's score 2 into when using

.execute('''INSERT INTO ScoresTable (name, score 1, score 2, score 3)''')

Do I use the column index but how would I implement that into the code?

Upvotes: 4

Views: 5035

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125368

To alter existing rows, you need to use an UPDATE statement, with a WHERE filter:

UPDATE ScoresTable SET ("score 1"=12, "score 2"=20, "score 3"=42) WHERE name='John'

I used ".." double quoting around the column names since they have spaces in them.

Using a cursor in Python, with SQL parameters, that'd translate to:

cursor.execute('''\
    UPDATE ScoresTable SET ("score 1"=?, "score 2"=?, "score 3"=?)
    WHERE name=?
    ''', (score1, score2, score3, name))

Upvotes: 6

Related Questions