简体   繁体   中英

How can I copy an sqlite database to memory?

I wanted to copy a ".db" file from an SQLite database to memory using JAVA code. How can I do this ?

What you could do is :-

  1. Open an in-memory database and attach the database you want to copy.

  2. Create the tables and copy the data for each table (you can ascertain the table and other entity names from sqlite_master (name, type and table columns))

    1. You could use CREATE TABLE x AS SELECT * FROM attached_database_identifier.x noting limitations as per [SQL As Understood By SQLite - CREATE TABLE - CREATE TABLE ... AS SELECT Statements] ( https://www.sqlite.org/lang_createtable.html#rowid ))
    2. the above is for the table name x
    3. Alternately you could create the tables based upon the SQL that is stored in sqlite_master in the sql column.
  3. Create the other entities Indexes, Views and Triggers (again the sql column of sqlite_master will hold the SQL to create the entities).

    1. Indexes would then be built accordingly.
    2. Triggers would then work (you wouldn't want to create them before the data has been copied from the original tables as the triggers would have done their work).
    3. Views extract the resultant data from the tables when used.
  4. detach the attached database.
  5. You now have the in-memory copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM