Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: Easiest way to copy a table from one database to another?

Tags:

sqlite

I have two sqlite databases and want to copy a table from database A to database B. The other tables in database A should not be copied. What is the easiest way to do that in Java?

I could definitively do something like Select * from A and then insert all of this into database B, but shouldn't there a better way?

like image 386
user6189 Avatar asked Mar 23 '15 21:03

user6189


People also ask

How do I copy a table from one database to another?

Right-click on the database name, then select "Tasks" > "Export data..." from the object explorer. The SQL Server Import/Export wizard opens; click on "Next". Provide authentication and select the source from which you want to copy the data; click "Next". Specify where to copy the data to; click on "Next".


2 Answers

Open the database you are copying from, then run this code to attach the database you are copying to and then copy a table over.

ATTACH DATABASE 'other.db' AS other;

INSERT INTO other.tbl
SELECT * FROM main.tbl;
like image 159
Colonel Thirty Two Avatar answered Oct 22 '22 03:10

Colonel Thirty Two


Why do you want to do that in Java? You can do it directly at the command-line, by dumping your table, and reading it into your other database :

sqlite3 A.sqlite ".dump some_table" | sqlite3 B.sqlite

If you need to drop the existing table in "B.sqlite" first, you can use the -cmd switch in the second part, to "run command before reading stdin" :

sqlite3 A.sqlite ".dump some_table" \
| sqlite3 -cmd "DROP TABLE IF EXISTS some_table" B.sqlite
like image 6
mivk Avatar answered Oct 22 '22 04:10

mivk