1

I have 2 sqlite databases, and I'm trying to insert data from one database to another. For example, "db-1.sqlite" has a table '1table' with 2 columns ('name', 'state'). Also, "db-2.sqlite" has a table '2table' with 2 columns ('name', 'url'). Both tables contain a list of 'name' values that are mostly common with each other but randomized, so the id of each row does not match.

I want to insert the values for the 'url' column into the db-1's table, but I want to make sure each url value goes to its corresponding 'name' value.

So far, I have done this:

> sqlite3 db-1.sqlite
sqlite> alter table 1table add column url;
sqlite> attach database 'db-2.sqlite' as db2;

Now, the part I'm not sure about:

sqlite> insert into 1table(url) select db2.2table.url from db2.2table where 1table.name==db2.2table.name

If you look at what I wrote above, you can tell what I'm trying to accomplish, but it is incorrect. If I can get any help on the matter, I'd be very grateful!!

1 Answer 1

5

The equality comparison operator in SQL is =, not ==.

Also, I suspect that you should be updating 1table, rather then inserting in it.

Finally, your table names start with digits, so you need to escape them.

This SQL should work better:

update `1table` 
set url = (select db2.`2table`.url 
           from db2.`2table` 
           where `1table`.name = db2.`2table`.name);
Sign up to request clarification or add additional context in comments.

1 Comment

ingenious! I am new to SQL. Thank you so much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.