Freek8
Freek8

Reputation: 704

Update from multiple databases in SQLite

I have two tables in two different databases. I'd like to update column data in db1 with column data in db2 based on id. db1 contains less rows than db2, so I do not want to copy over the whole table. I only want to update the data column of the already existing rows in db1.

I'm using SQLite (and am restricted to using this). Is there a query I can use that does the above?

Upvotes: 3

Views: 1557

Answers (1)

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26599

With db1 open, try this:

ATTACH 'path/to/db2' AS db2;
UPDATE main.table
SET data = (SELECT data FROM db2.table WHERE main.table.id = db2.table.id)
WHERE EXISTS(SELECT 1 FROM db2.table WHERE main.table.id = db2.table.id);

Upvotes: 8

Related Questions