【问题标题】:How to retrieve data from Firebase Realtime Database to mySqlite?如何从 Firebase 实时数据库检索数据到 Sqlite?
【发布时间】:2020-10-12 22:45:05
【问题描述】:

上图显示了我的 Firebase 数据库的数据库结构,这就是在我的 Firebase 实时数据库中保存费用的方式。如何借助下面给出的数据模型从 firebase 获取该数据并将其存储在我的 android 应用程序的 SQlite 数据库中?

public class Expense {
    public String id;
    public String amount;
    public String description;

    public Expense() {}

    // [START post_to_map]
    @Exclude
    public Map < String, Object > toMap() {
        HashMap < String, Object > result = new HashMap < > ();
        result.put("id", id);
        result.put("amount", amount);
        result.put("description", description);
        return result;
    }
    // [END post_to_map]


    public Expense(String id, String amount, String description) {
        this.id = id;
        this.amount = amount;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

这是我用来向 SQLITE 数据库添加数据的函数

 public boolean addExpense(Expense expense) {
     ContentValues contentValues = new ContentValues();
     contentValues.put("expense_id", expense.getId());
     contentValues.put("expense_amount", expense.getAmount());
     contentValues.put("expense_description", expense.getDescription());
     SQLiteDatabase db = getWritableDatabase();
     db.insert(DBHelper.expenses_table, null, contentValues);
     return true;
 }

如何将实时数据库中的数据保存到 SQLite 数据库?

【问题讨论】:

  • 这段代码中到底有什么不符合您的预期?您从哪里获取 Firebase 的数据?

标签: android firebase sqlite firebase-realtime-database


【解决方案1】:

你可能会看看这个答案。它是相关的 save List to sqlite

要将数据从 firebase 实时数据库保存到 sql 数据库,您应该将 ValueEventListener 添加到存储费用数据的 DatabaseReference 并遍历每个费用数据并将其传输到 addExpense() 函数。

查看此示例

您的费用数据模型:

public class Expense {
    public String id;
    public String amount;
    public String description;

    public Expense() {
    }

    // [START post_to_map]
    @Exclude
        public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("amount", amount);
        result.put("description", description);
        return result;
    }
    // [END post_to_map]


    public Expense(String id, String amount, String description) {
        this.id = id;
        this.amount = amount;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

在这里,您尝试从 firebase 收集费用清单,那么您可能需要使用此方法:

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("path_to_your_expenses_reference");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot expensedata: dataSnapshot.getChildren()) {
            Expense expense = expensedata.getValue(Expense.class);
            addExpense(expense);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("The read failed: " + databaseError.getCode());
    }
});

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 2019-05-30
    • 2019-04-16
    • 2020-12-09
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多