29

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?

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
user6189
  • 653
  • 1
  • 7
  • 15
  • 2
    Possible duplicate of [Copying data from one sqlite db to another](http://stackoverflow.com/questions/2359205/copying-data-from-one-sqlite-db-to-another) – Jukka Suomela Mar 08 '16 at 18:38

2 Answers2

39

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;
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
6

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
mivk
  • 13,452
  • 5
  • 76
  • 69