如何在Android中使用SQLiteDatabase一次将数据插入两个表中? [英] How to insert data into two tables at once using SQLiteDatabase in Android?

查看:302
本文介绍了如何在Android中使用SQLiteDatabase一次将数据插入两个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以可以说我有这段代码:

So lets say I have this code:

SQLiteDatabase sqlDB = database.getWritableDatabase();
long iD = 0;
iD = sqlDB.insert(Table1, null, TestV);

我能以某种方式重写它以插入到两个表而不是一个表中吗?我的问题是值从一个表返回3个值,而从另一个表返回1个值。因此,表1中没有的1值会引发一个错误,即在表1中找不到该列。有没有办法做到这一点?如果不是,那我应该做两个单独的插入物吗?我很困惑如何将数据插入一个数据库中的两个表中(表是关系型的:一个具有外键)。

Can I somehow rewrite this to insert into two tables instead of one? My problem is that 'values' returns 3 values from one table, and 1 value from another. Therefore, the 1 value that isn't in 'Table1' sets off an error that the column cant be found in 'Table1.' Is there a way to do this? If not then should I do two separate inserts? I am confused as how to insert data into two tables in one database (Tables are Relational: one has a foreign key).

推荐答案

文档中在SQLite中进行INSERT:

From the documentation for INSERT in SQLite:

请注意,流程图不会给您机会一次发布到多个表中。 SQLiteDatabase.insert()是使用INSERT语句的便捷方法。

Note that the flowchart doesn't give you an opportunity to post into more than one table at once. SQLiteDatabase.insert() is a convenience method for using the INSERT statement.

这篇关于如何在Android中使用SQLiteDatabase一次将数据插入两个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆