SQLite on iOS - SQL to copy from one file to anoth

2019-04-01 03:53发布

问题:

I have 2 database files in my iOS app:

  • An original database (readonly, kept in my app's home directory)
  • The user's database (kept in the user's Documents folder)

I need to run a SQL query to copy from the original database to the user's, something like:

INSERT INTO UserDatabase.MyTable
SELECT * FROM OriginalDatabase.MyTable;

Is this possible from SQLite running on iOS? The problem is the two databases are in different folder. I would like to avoid doing this work in code (C#) which is the obvious way to do it, just much slower.

My app is written with C#/MonoTouch, but that is probably irrelevant.

回答1:

Solved with @Peter M's link.

Here is the SQL to do this on iOS:

    ATTACH DATABASE '../YourApp.app/YourDatabase.db' AS OriginalDatabase;

    INSERT INTO Main.MyTable
    SELECT * FROM OriginalDatabase.MyTable;

    DETACH DATABASE OriginalDatabase;

Note the use of Main, this is the database you are connected to with SqliteConnection, in my case it was located in the Documents folder.

You can do anything you need against the OriginalDatabase, joins, subselects, etc.