如何在Android Sqlite中关联两个表?
我需要帮助,以使用SQLite在Android Studio中的两个表之间建立关系。我有一个表是给用户的,另一个是用于用户可以保存的联系人,
user_table(userId, fullName, email, password, phoneNumber)
contacts_table(contactId, contactName, contactPhoneNumber)
我需要让用户输入尽可能多的联系人,因此,当他登录到会话时,将显示所有存储的联系人那个用户。 我无法弄清楚如何在表之间建立关系,然后结束它创建两个分隔的表,因此不同的用户可以看到相同的联系人。
I need help to create the relation between two tables in Android Studio using Sqlite. I have one of the tables is for users and the other one is for contacts that the user can saved
user_table(userId, fullName, email, password, phoneNumber)
contacts_table(contactId, contactName, contactPhoneNumber)
I need to let the user enter as many contacts as it wants to, so when he login into its session will display all the contacts stored under that user.
I couldn't figure it how to make the relation between the tables, and end it up create two separated tables, so different user can see the same contacts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您说“用户输入任意数量的联系人“ ,然后说”,因此不同的用户可以看到相同的联系人” 。
在这种情况下,可以使用许多人的关系。
这涉及使用 3rd表将用户映射到合同以及与用户的联系。因此,用户可以拥有许多联系人,并且联系人可以有很多用户。
这样的表有许多名称,例如关联表,映射表,参考表。...
这样的表具有两个列(或更多),其中一列将参考/地图保存给用户,另一个则是参考/参考/参考/映射到联系人。地图/参考是唯一标识用户以及联系人的东西。看来UserID和ContactID可能具有此属性。
但是,您可以例如
,这很容易引起参考/映射。...不正确,因此您可以通过使用外键约束来引入规则,从而强制执行引用(参考完整性)的完整性(参考完整性)。
因此,例如,例如,您可以说: -
例如,这说明userId_map列的值必须是User_table的用户ID列中存在的值,否则会引发错误。
除了此相反/规则外,删除级联(还有其他选项,但级联选项可能是最有用的),如果将父母(分别分别为user_table行或contacts_table行分别删除),则将删除将删除给孩子们,并且因此,将删除USER_CONTACT_MAP中的行。
如果是“更新级联”,则如果更改了UserID或ContactID,则将更改将其级联归为user_contact_map表中的孩子。
作为上述功能的示例考虑: -
此运行将导致: -
例如,您也可以使用类似的内容: -
以下是一个工作示例,反映了上面显示的内容。最终在两个结果(上面的屏幕图像)中,但写入日志。
首先是 databasehelper (扩展了SQLiteOpenhelper的类),该代码与创建表的数据库相关的所有代码,为插入数据插入以及返回光标的两个提取物提供功能: -
第二是一种活动 MainAttivity ,它插入数据,然后提取数据库helper的数据: -
当运行(第一次)时(首次运行),然后根据日志,结果为: -
You say that "user enter as many contacts as it wants to" and then say "so different user can see the same contacts".
In this case a many-many relationship can be used.
This involves using a 3rd table that maps a user to a contract and also a contact to a user. Thus a user can have many contacts and a contact can have many users.
Such a table has many names such as an associative table, a mapping table, a reference table ....
Such a table has two columns (or more) one of which holds a reference/map to the user and the other a reference/map to the contact. The map/reference being something that uniquely identifies the user and also the contact. It would appear that userId and contactId probably have this attribute.
So you could for example have
However, this is prone to references/mappings .... being incorrect so you can introduce rules that enforce the integrity of the references (referential integrity) by the use of Foreign Key constraints.
So instead you could, for example, have :-
This, for example, says that the value of the userId_map column MUST be value that exists in the userId column of the user_table otherwise an error is raised.
In addition to this contraint/rule the ON DELETE CASCADE (there are other options but CASCADE option is perhaps the most useful) says that should the parent (user_table row or contacts_table row respectively) be deleted then the deletion will be cascaded to the children and thus that the rows in the user_contact_map will be deleted.
In the case of ON UPDATE CASCADE, then if the userId or the contactId is changed, then that change will be cascaded to the children in the user_contact_map table.
As an example of the functionality described above consider:-
This when run will result in :-
You could also, for example use something like :-
Which would result in :-
Working Example in Android
The following is a working example that reflects what is shown above. Culminating in the two results (screen images above) but written to the log.
First is the DatabaseHelper (class that extends SQLiteOpenHelper) which has all the code relevant to the database that creates the tables, provides functions for the insertion of data and for the two extracts that return a Cursor:-
Second is an activity MainActivity that inserts the data and then extracts data utilising the DatabaseHelper:-
When run (for the first time) then the results, as per the log, are:-
使用外键约束。为此,您需要在父表中拥有一个主键(此处为'user_table'),然后在子表中定义外键约束(herere'contacts_table')。
这是您这样做的SLQITE脚本:
因此,您应该能够在Contacts_table中的User_FK列的帮助下区分两个用户的联系(这表明该特定行是哪个用户的联系人),
谢谢您
!
Use the foreign key constraint. To do so you need to have a primary key in your parent table (here 'user_table') and then define a foreign key constraint in your child table (here 'contacts_table').
Here's your slqite Script to do so:
So you should be able to distinguish between the contacts of two users with the help of the user_fk column in contacts_table (it would indicate which user's contact that particular row is)
Thank you
Over and out!