插入两个相互引用的SQL表,而无需执行单独的查询
Inserting into two SQL tables that reference each other without having to execute separate queries?
所以我有两个sqlite表(使用c++管理),如下所示:
Table A:
primaryKey (Primary key auto increment)
b_ref (int REFERENCES TableB(primaryKey))
added_info (text)
和
Table B:
primaryKey (Primary key auto increment)
info (text)
让我们说,定期我想插入一些东西到B,然后总是插入一些东西到a .是否有一种方法,我可以插入到表B,并有sql返回主键,这样我可以快速插入到a,而不必在表B上运行查询?
简单来说,我现在做的是:
Insert into A.
QUERY A to find the primary key of what I just inserted.
Insert into B (passing the results from the above query)
我想做的是:
Insert into A and retain the primary key for this insert
Insert into B (passing the primary key from the above insert)
这样的事情可能吗?如果我手动定义主键是什么(而不是使用自动递增),我可以将这个手动定义的值传递给两个插入吗?
如果不使用自动增量特性,则可以自己管理主键,只需将相同的值传递给两个方法。这样做的唯一缺点是,您现在必须知道哪些主键可用于执行插入。
如果表a和表B之间是一对一的关系,那么将它们合并成一个表并一次将所有数据输入到该表中可能会更有效。
SQLite有函数sqlite3_last_insert_rowid:
sqlite3_last_insert_rowid(D)接口返回数据库连接D上最近一次成功插入到rowid表或虚拟表中的rowid。
或者,您可以使用last_insert_rowid SQL函数
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 如何在C++中从两个单独的for循环中添加两个数组
- 查询SQLite数据库中的日期
- 用C++在单独的头文件中完成函数体
- 类模板的成员功能的定义在单独的TU中完全专业化
- 如何使用单独文件中的派生类访问友元函数对象
- CoInitialize()在单独的线程上崩溃而不返回
- 如何在ArangoDb AQL查询中指定数据库
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- Qt SQLite没有查询或参数计数不匹配
- 如何使用c++在VS 2019上运行SQL查询
- 从返回的顶点缓冲区查询顶点结构
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 单独定义模板化嵌套类方法的正确语法
- 如何将 2 个类分成单独的 .h 文件并正确设置它们
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 以非特权用户身份查询 NTFS 特殊文件的元数据?
- C/C++ - 查询平台相关的换行符(用于内存映射文件)
- 查询事件日志.如何查询才能单独获取第一个和最后一个事件?
- 插入两个相互引用的SQL表,而无需执行单独的查询