Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple database operations from sql query

Tags:

android

I have two instances of SQLiteDatabase database. And I need to copy data from one to another. I need to execute this query:

INSERT INTO `toDB`.`tableName` SELECT * FROM `fromDB`.`tableName` 

so, How can I do this my database instances? How to replace toDB and fromDB ?

like image 701
pleerock Avatar asked Dec 06 '25 03:12

pleerock


1 Answers

Never tried that but it should work this way:

you have to ATTACH the other database at SQLite level so sqlite can access it directly.

For example you open the database that shall be the toDB and you issue the following command (via execSQL)

ATTACH DATABASE '/data/data/your.package/databases/dbname.db' AS fromDB

you should have access to the other database now and can do

INSERT INTO main.tableName SELECT * FROM fromDbB.tableName

the database you opened originally has the name "main" automatically.

You can and should get the path to your databases via Context#getDatabasePath since there is no guarantee that this path is the same on all devices.

like image 136
zapl Avatar answered Dec 08 '25 16:12

zapl