JJFord3
JJFord3

Reputation: 1985

Is it possible to both insert and update in a single sqlite statement?

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

Answers (2)

CL.
CL.

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

Dharmesh Fumakiya
Dharmesh Fumakiya

Reputation: 2338

yes,you can use

INSERT OR REPLACE INTO table(column_list)
VALUES(value_list);

Upvotes: 0

Related Questions