在 SQLite 中更新多个相关表

发布于 2024-08-27 02:25:06 字数 1056 浏览 2 评论 0原文

只是一些背景知识,抱歉啰嗦了这么久。

我正在使用 System.Data.SQLite ADO.net 适配器创建本地 sqlite 数据库,这将是访问数据库的唯一进程,因此我不需要担心并发性。

我正在从各种来源构建数据库,并且不想使用数据集或数据适配器或类似的东西在内存中构建所有这些。我想使用 SQL (DdCommands) 来执行此操作。我不太擅长 SQL,对 sqlite 完全是菜鸟。我基本上使用 sqlite 作为本地数据库/保存文件结构。

数据库有很多相关的表,数据与 People 或 Regions 或 Districts 无关,但用一个简单的类比,想象一下:

具有自动增量 RegionID、RegionName 列和各种可选列的 Region 表。

具有自动增量 DistrictID、DistrictName、RegionId 和各种可选列的 District 表

具有自动增量 PersonID、PersonName、DistrictID 和各种可选列的 Person 表

因此,我得到了一些表示 RegionName、DistrictName、PersonName 和其他与 Person 相关的数据的数据。此时可能会也可能不会创建地区、地区和/或个人。

再说一次,我的想法是这样的:

  • 检查 Region 是否存在,如果存在,则获取 RegionID,
  • 否则创建它并获取 RegionID
  • 检查 District 是否存在,如果存在,则获取 DistrictID,否则
  • 创建它从上面添加 RegionID 并获取 DistrictID
  • 检查 Person 是否存在,如果存在,则获取 PersonID,否则
  • 创建它,从上面添加 DistrictID 并获取 PersonID
  • 使用其余数据更新 Person。

在 MS SQL Server 中,我将创建一个存储过程来处理所有这些。

我能看到使用 sqlite 执行此操作的唯一方法是使用大量命令。所以我确信我没有明白这一点。我花了几个小时在各个网站上四处查看,但就是觉得自己走的路不对。任何建议将不胜感激。

Just some background, sorry so long winded.

I'm using the System.Data.SQLite ADO.net adapter to create a local sqlite database and this will be the only process hitting the database, so I don't need to worry about concurrency.

I'm building the database from various sources and don't want to build this all in memory using datasets or dataadapters or anything like that. I want to do this using SQL (DdCommands). I'm not very good with SQL and complete noob in sqlite. I'm basically using sqlite as a local database / save file structure.

The database has a lot of related tables and the data has nothing to do with People or Regions or Districts, but to use a simple analogy, imagine:

Region table with auto increment RegionID, RegionName column and various optional columns.

District table with auto increment DistrictID, DistrictName, RegionId, and various optional columns

Person table with auto increment PersonID, PersonName, DistrictID, and various optional columns

So I get some data representing RegionName, DistrictName,PersonName, and other Person related data. The Region, District and/or Person may or may not be created at this point.

Once again, not being the greatest with this, my thoughts would be something like:

  • Check to see if Region exists and if so get the RegionID
  • else create it and get RegionID
  • Check to see if District exists and if so get the DistrictID
  • else create it adding in RegionID from above and get DistrictID
  • Check to see if Person exists and if so get the PersonID
  • else create it adding in DistrictID from above and get PersonID
  • Update Person with rest of data.

In MS SQL Server I would create a stored procedure to handle all this.

Only way I can see to do this with sqlite is a lot of commands. So I'm sure I'm not getting this. I've spent hours looking around on various sites but just don't feel like I'm going down the right road. Any suggestions would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-09-03 02:25:06

last_insert_rowid()INSERT OR REPLACE 结合使用。像这样的东西:

INSERT OR REPLACE INTO Region (RegionName)
                       VALUES (:Region   );

INSERT OR REPLACE INTO District(DistrictName, RegionID           )
                        VALUES (:District   , last_insert_rowid());

INSERT OR REPLACE INTO Person(PersonName, DistrictID           )
                      VALUES (:Person   , last_insert_rowid());

Use last_insert_rowid() in conjunction with INSERT OR REPLACE. Something like:

INSERT OR REPLACE INTO Region (RegionName)
                       VALUES (:Region   );

INSERT OR REPLACE INTO District(DistrictName, RegionID           )
                        VALUES (:District   , last_insert_rowid());

INSERT OR REPLACE INTO Person(PersonName, DistrictID           )
                      VALUES (:Person   , last_insert_rowid());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文