I have a JSON object containing sets of a "keyX" and the corresponding "value".
data = {
"key1": 10,
"key2": 20,
...
}
I need to write the values into a database into the column "keyX". Unfortunately one can't format the SQL Query like this:
for key in data.keys():
cur.execute('UPDATE table SET ?=? WHERE identifier=?;', (key, data[key], identifier))
Therefore I'm currently solving it like this:
for key in data.keys():
cur.execute('UPDATE table SET ' + key + '=? WHERE identifier=?;', (data[key], identifier))
This is working perfectly, but SQL queries shouldn't be constructed string-based. In this specific case, the keys are not set by the user, so SQL injection by the user is imo not possible,
Can this be solved better without string-based query construction?