SQLite 从数据转储脚本导入数据,包括主键和外键
我有一个使用外键的 SQLite 数据库,其中一些将是自动增量值。系统代表的“核心”数据例如是一辆汽车。例如,外键链接到有关车轮和轮胎的信息,我希望从一个数据库导出 n 辆车并导入到另一个数据库。
我想通过编写一组可以由导入数据库加载的sql语句(即一堆insert语句)来做到这一点,但是转储数据中的键值不一定与现有数据匹配(也许有某些关键值重复)。
处理这个问题的最佳方法是什么?是否有一种简单或推荐的方法来编写导入脚本,以便消除对导出键值的依赖?
在下面的示例中,carindex 将命名一辆汽车。 CarPartColours 将单个零件与颜色定义链接起来。 CarPartColours 中将有多行具有相同的 CarID。 我希望当用户在 carindex 中选择一行时导出 carpartcolours、carindex、parts 和 color 中的所有相关行,并导入到另一个数据库中。该数据库中的颜色定义可能是重复的(另一个不同的问题)或者与原始数据库中的颜色定义具有相同的键值。
CREATE TABLE carindex (
ID integer PRIMARY KEY NOT NULL,
Name varchar(50)
);
CREATE TABLE carpartcolours (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
CarID integer,
PartID integer,
ColourID integer,
/* Foreign keys */
FOREIGN KEY (CarID)
REFERENCES carindex(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
FOREIGN KEY (PartID)
REFERENCES parts(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
FOREIGN KEY (ColourID)
REFERENCES colours(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE colours (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
Name varchar(50),
R real,
G real,
B real
);
CREATE TABLE parts (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
Name varchar(50),
Value real,
Manufacturer varchar(50)
);
I have a SQLite database that makes use of foreign keys, some of which will be autoincremented values. The "core" data the system represents is for example a car. The foreign keys are linking to information about wheels and tyres for example, and I wish to export n cars from one database and import into another.
I want to do this by writing a set of sql statements (i.e a bunch of insert statements) that can be loaded by the importing database, but the key values in the dumped data will not necessarily match up with the existing data (maybe there are duplicates in some of the key values).
What is the best way to deal with this? Is there an easy or recommended way to write the import script so that dependencies on exported key values are removed?
In the example below, a carindex will name a car.
CarPartColours links a single part and with a colour definition. There will be multiple rows in CarPartColours with the same CarID.
I wish to export all the relevant rows from carpartcolours, carindex, parts and colours when the user selects a single row in carindex, and import into another database. The colour definitions in that database may be duplicates (another different issue) or have the same key values as those in the origin db.
CREATE TABLE carindex (
ID integer PRIMARY KEY NOT NULL,
Name varchar(50)
);
CREATE TABLE carpartcolours (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
CarID integer,
PartID integer,
ColourID integer,
/* Foreign keys */
FOREIGN KEY (CarID)
REFERENCES carindex(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
FOREIGN KEY (PartID)
REFERENCES parts(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
FOREIGN KEY (ColourID)
REFERENCES colours(ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE colours (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
Name varchar(50),
R real,
G real,
B real
);
CREATE TABLE parts (
ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
Name varchar(50),
Value real,
Manufacturer varchar(50)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Mike我发布了之前的答案,我之前的思路完全错误,所以我重新开始。我很抱歉。
我想说你需要确保你研究了数据库主从复制,因为这就是你想要做的。您想要将数据从从站复制到主站。由于您不知道哪个插入的位置何时,那么您需要寻找无冲突的密钥(或尝试无冲突的密钥)。因此,因为您可以在任何一个数据库中生成一条记录,并且可以将该记录迁移到任何其他数据库,所以您需要生成一个 UUID 样式键,并使用它来代替 INT AUTOINCRMENT。
这是跨数据库数据复制的唯一方法。
否则,您只想最后插入到
carpartcolours
中。很抱歉延迟回答您的问题...
@Mike I posted a previous answer, and I was on a totally wrong train of thought before, so I'm starting fresh. My apologies.
I would say that you need to make sure you look into database master slave replication, as that's what you're trying to do. You want to replicate the data from the slave to the master. Since you're not going to know which was inserted where when, then you need to look for a collision free key (or try for something collision free). So because you may generate a record in any one database and you may migrate that record to any other database, then you want to generate a UUID style key, and use that in place of a INT AUTOINCREMENT.
This is the only way to do cross database data replication.
Otherwise, you just want to insert into
carpartcolours
last.Sorry for the delay in answering your question ...
尝试将所有插入语句包装到事务中:
Try wrapping all your insert statements into transaction:
我本身不熟悉sqlite,但我过去遇到类似问题时所做的是:
这在 sqlite 中可能是不可能的,或者可能不起作用,因为您具有具有相同主键的预先存在的数据。
在这种情况下,您可以将数据从源数据库导入到临时表中,然后编写脚本,从依赖链的“最远”端开始,使用手写 SQL 将数据导入到目标数据库中。不过还是蛮费力的
I'm not familiar with sqlite per se, but what I have done in the past with similar problems is:
This may not be possible in sqlite, or may not work because you have pre-existing data with the same primary keys.
In this case, you can import the data from the origin database into temporary tables, and then write scripts to import this into the target database with hand-written SQL, starting at the "furthest" end of the dependency chain. Quite laborious, though.