Reputation: 1985
I want to insert a row from a table into a new table but change one value as I do so. Is it possible to do this in one statement?
Currently I have:
db = sqlite3.connect('C:/Desktop')
db.execute("""insert into table2 select * from table1 where
ID = {0}""".format(5))
db.execute("""update table2 set column = {0} where ID = {1}""".format("new string",5)
db.commit()
db.close()
Upvotes: 1
Views: 98
Reputation: 180040
Replace *
with the actual column list, and then replace that column with the new value:
INSERT INTO Table2
SELECT ThisColumn, ThatColumn, 'new string', OtherColumns
FROM Table1
WHERE ID = ?;
Upvotes: 3
Reputation: 2338
yes,you can use
INSERT OR REPLACE INTO table(column_list)
VALUES(value_list);
Upvotes: 0