How to dump SQLite database in memory to file using ADO.NET?

I am using System.Data.SQLite.dll to use the SQLite database in memory. After completing the program, I want to dump the database in memory into a .db3 file for later use. How can I achieve this in C #?

+6
source share
1 answer

As far as I know, there are no built-in functions for System.Data.SQLite.dll. However, functionality exists in the sqlite3.exe client, which is supported with the SQLite kernel.

Here is how I would do it with system.data.sqlite.dll:

  • Get SQL statements to create a new database structure.

     select sql from sqlite_master where name not like 'sqlite_%'; 
  • Get the names of all user tables.

     select name from sqlite_master where type='table' and name not like 'sqlite_%'; 
  • Create a database on disk in some new SQLiteConnection.

  • Executing all previously received SQL statements to create the database structure in the database on disk.

  • Close the separate database connection on disk.

  • Attach the database on disk to the database in memory.

     attach 'ondisk.db3' as 'ondisk'; 
  • For each user table obtained earlier, copy the contents from memory to the database on disk.

     insert into ondisk.TableX select * from main.TableX; insert into ondisk.TableY select * from main.TableY; 
+5
source

Source: https://habr.com/ru/post/924254/


All Articles