如何在Firebase中插入两个表的数据? [英] How to insert data of two tables in Firebase?
问题描述
我是Firebase的新手,我打算从SQLite切换到Firebase.
I am new to Firebase and I am planning to switch from SQLite to Firebase.
我在SQLite中有2个表.
I have 2 tables in SQLite.
-
第一个是
存储库名称
.对于所有用户而言,这将是通用的,应用程序的所有用户都可以访问.
First one is
NAME OF REPOSITORIES
. This will will be general for all users which is accessible to all the users of the application.
第二个是 BOOKMARKS
.这将特定于特定用户.我知道Firebase中没有表结构.
Second one is BOOKMARKS
. This will be specific to a particular user . I know that there is no table structure in Firebase .
问题是,如何在Firebase中构造此类数据?我曾尝试了解Firebase的树结构,但没有得到有关这种情况的帮助.
Question is, how can I structure this type of data in Firebase? I have tried understanding tree structure of Firebase, but didn't get help for this type of scenario.
推荐答案
要创建两个表,必须为第二个表创建 DatabaseReference
对象.另外,您还必须创建第二个节点.
To create two tables you have to create object of DatabaseReference
for second table. Also you have to create second node.
public class MainActivity extends AppCompatActivity {
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
//Create an object of DatabaseReference to create second table
private DatabaseReference mFirebaseDatabase1;
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseInstance = FirebaseDatabase.getInstance();
// get reference to 'RepositoryName' node
mFirebaseDatabase = mFirebaseInstance.getReference("RepositoryName");
// get reference to 'Bookmarks' node
mFirebaseDatabase1 = mFirebaseInstance.getReference("Bookmarks");
// Save / update the user
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = inputName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String confirmPassword = inputConfirmPassword.getText().toString();
// Check for already existed userId
if (TextUtils.isEmpty(userId)) {
createUser(name, email, password, confirmPassword);
}
}
});
}
/**
* Creating new user node under 'users'
*/
private void createUser(String name, String email, String password, String confirmPassword) {
// TODO
// In real apps this userId should be fetched
// by implementing firebase auth
if (TextUtils.isEmpty(userId)) {
/*userId store the unique key like KYNGnlMMIf3w11VukqD
in this key store usename and email as JSON format in firebase cloud database*/
// "mFirebaseDatabase" is for table1(i.e RepositoryName)
//userId = mFirebaseDatabase.push().getKey();
// "mFirebaseDatabase1" is for table2(i.e Bookmarks)
userId = mFirebaseDatabase1.push().getKey();
}
User user = new User(name, email, password, confirmPassword);
//insert data in firebase database RepositoryName
mFirebaseDatabase.child(userId).setValue(user);
//insert data in firebase database Bookmarks
mFirebaseDatabase1.child(userId).setValue(user);
}
}
希望这对您有所帮助:)
Hope this helps you :)
这篇关于如何在Firebase中插入两个表的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!