每日插入到 Firebase 数据库中。

huangapple go评论72阅读模式
英文:

Inserting daily into firebase database

问题

我有这样一个活动,我需要每天将数据插入数据库,但在每天插入数据时不应删除前一天的数据,这意味着我应该能够在我想要的时候在我的应用程序中查看所有数据。例如:用户必须将他们每天的餐饮输入到应用程序中,这将更新到数据库,但他们也希望查看他们昨天输入的餐饮。我如何使用 Firebase 数据库实现这一点?

英文:

I have this activity where I need to insert data daily into the database but when inserting it daily it must not delete the previous days data so that means, I should be able to view all my data from the database in my app when I would like too. For example: the user must input their daily meals into the app which will update to the database but they would also like to view their meals that they input yesterday. How would i go about doing this using firebase database?

答案1

得分: 1

每次向 Firebase 数据库插入数据时,只需创建一个随机键,这样就不会覆盖先前的值。

// 获取推送键值
String key = mDatabase.child("posts").push().getKey();

// 然后可以按照以下方式在该节点中写入数据
mDatabase.child("posts").child(key).setValue(yourValue);
英文:

For everytime you insert a data into the Firebase database , just create a random key , in that case it wont overide the previous value

    //get the push key value
String key = mDatabase.child("posts").push().getKey();

//then you can write in that node in this way
mDatabase.child("posts").child(key).setValue(yourValue)

答案2

得分: 0

你可以尝试创建类似这样的代码:

FirebaseFirestore
    .collection(你的集合名称)
    .document(System.currentTimeMillis())// 这里是文档的 ID
    .set(你的餐饮对象(args...))

System.currentTimeMillis() 应该为你的文档提供唯一的 ID。

英文:

You can try to make something like this

 FirebaseFirestore
            .collection(YOUR_COLLECTION_NAME)
            .document(System.currentTimeMillis())// here comes the id of the document
            .set(YourMealObject(args...))

System.currentTimeMillis() should give you unique id's for your document

huangapple
  • 本文由 发表于 2020年9月28日 17:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64099181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定