Dev Python
Dev Python

Reputation: 185

Update column without overwriting content in sqlite3

I need to update a column without overwriting its content. I tried with the sqlite3 syntax 'concat' which is ||, but it didn't work. This way the data is not saved and the column remains empty.

data = ('Hello!', 1)
sql = """UPDATE dbase SET observation = observation || ? WHERE id = ?;"""
c = conn.cursor()
c.execute(sql, data)
conn.commit()
conn.close()

Any idea?

Upvotes: 0

Views: 247

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

and the column remains empty

If the current of observation be NULL, then concatenating anything to it will also just generate NULL. One option might be:

data = ('Hello!', 1,)
sql = "UPDATE dbase SET observation = COALESCE(observation, '') || ? WHERE id = ?;"
c = conn.cursor()
c.execute(sql, data)

Upvotes: 1

Related Questions