Saturday 2 August 2014

Copy table from one SQLite database to another

If you are anything like me, you like clear instructions for doing things. This is especially true when it comes to figuring things out on the computer. 

Unfortunately, explanations that are easy to follow often lack when it comes to programmers. 

The other day I came across a problem where I wanted to move a table in a SQLite-database to another SQLlite-database in my RubyOnRails-learning project. 

Now, there are explanations out on the internet, but they require prior knowledge, knowledge that I don't have. So it took a bit of hand wringing before I figured it out.

This is why I thought that I should write a blog post about it where I explain everything in detail.

Copying table from one SQLite database to another:

1./ Open the primary database in the root of your RailsApp:

$  SQLite3 db/primaryDB

You should now have entered SQLite.

SQLite>  

2./ Then attach the database that you would like to copy from:

SQLite> ATTACH 'db/secondaryDB' AS SampleDB;

3./ Now you can have a look at what you've achieved so far:

SQLite> .databases;
SEQ  NAME                FILE
0        Main                 db/primaryDB
2        SampleDB        db/secondaryDB

4./ Here comes the rub:

SQLite> INSERT INTO table_A SELECT * FROM SampleDB.table_B;

Hope this helps!









No comments:

Post a Comment