Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sqlite

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?

like image 677
rskw00 Avatar asked Nov 05 '25 23:11

rskw00


1 Answers

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))
like image 123
Martijn Pieters Avatar answered Nov 07 '25 12:11

Martijn Pieters