简体   繁体   English

从其他SQLite数据库C#创建SQLite数据库

[英]Create SQLite Database From Other SQLite Database C#

I have an SQLite database which a set of tables. 我有一个SQLite数据库,其中包含一组表。 All data in these tables can be isolated into a groups of sets by some id. 这些表中的所有数据都可以通过某些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 . 每个ID列将映射另一个ID ,每个ID2将映射到另一个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 : 我想使用此数据库,并生成一系列较小的数据库,每个数据库都具有相同的结构,但只包含来自1个ID数据:

Database1.sqlite 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 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. 我可以一个一个地创建表,通过一系列SELECT语句收集每个ID所有数据,然后通过一系列INSERT语句添加它,但是我认为必须有更好的方法。

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. 从那里,我可以将这些系列视图写成一个sqlite文件作为数据库。

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? 是否有可能生成一系列模仿每个表结构的视图,但是假设ID = 1 ,然后将这些视图另存为sqlite文件? All of this will need to be done in C#. 所有这些都需要在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. 更多信息这些表可以有多个具有相同ID的行。 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 . 可以结合使用INSERTSELECT Together with ATTACHed databases , this allows to do the copying with one statement per table: ATTACHed数据库一起使用,这允许对每个表使用一个语句进行复制:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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