简体   繁体   中英

Create SQLite Database From Other SQLite Database C#

I have an SQLite database which a set of tables. All data in these tables can be isolated into a groups of sets by some id. For example:

Table A
ID  value1 value2
1   asd    fgh
2   sdf    ghj

Table B
ID  ID2 value4
1   10   vbn
2   11   bnm

Table C
ID2 value5 value6
10  asdfg  qwer
11  tyui   hjkl

Where each ID column will map the other ID and each ID2 will map to the other ID2 .

I want to take this database, and generate a series of smaller databases, each of which have the same structure, but will only contain data from 1 ID :

Database1.sqlite

Table A
ID  value1 value2
1   asd    fgh

Table B
ID  ID2 value4
1   10   vbn

Table C
ID2 value5 value6
10  asdfg  qwer

Database2.sqlite

Table A
ID  value1 value2
2   sdf    ghj

Table B
ID  ID2 value4
2   11   bnm

Table C
ID2 value5 value6
11  tyui   hjkl

I could just create the tables one by one, gather all data per ID through a series of SELECT statements, then add it through a series of INSERT statements, but I think there has to be a better way.

My other idea is that I can create a series of views, each of which isolates the data into the format above. From there, I could just write these series of views an sqlite file as a database.

My question is how realistic is my view generation idea? Would it be possible to generate a series of views that mimic each table's structure, but for say where ID = 1 and then save those views as an sqlite file? All of this will need to be done in C#. Is there a better way to do what I am trying to do?

Some More Info These tables can have multiple rows with the same IDs. There will also need to be some primary key / foreign keys for each table. Ideally, we could then take these smaller tables, and then compress them all into a larger table in the future.

It is possible to combine INSERT and SELECT . Together with ATTACHed databases , this allows to do the copying with one statement per table:

ATTACH 'C:\some\where\Database1.sqlite' AS db1;
CREATE TABLE db1.A(ID, value1, value2);
CREATE TABLE db1.B(ID, ID2, value4);
CREATE TABLE db1.C(ID2, value5, value6);
INSERT INTO db1.A SELECT * FROM main.A WHERE ID = 1;
INSERT INTO db1.B SELECT * FROM main.B WHERE ID = 1;
INSERT INTO db1.C SELECT * FROM main.C
                  WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 1);

ATTACH 'C:\some\where\Database2.sqlite' AS db2;
CREATE TABLE db2.A(ID, value1, value2);
CREATE TABLE db2.B(ID, ID2, value4);
CREATE TABLE db2.C(ID2, value5, value6);
INSERT INTO db2.A SELECT * FROM main.A WHERE ID = 2;
INSERT INTO db2.B SELECT * FROM main.B WHERE ID = 2;
INSERT INTO db2.C SELECT * FROM main.C
                  WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 2);

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