如何将数据从SQLite数据库发送到Firebase数据库

如何将数据从SQLite数据库发送到Firebase数据库

本文介绍了如何将数据从SQLite数据库发送到Firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用,该应用将名称,数字之类的数据存储到SQLite数据库.我需要将数据从SQLite推送到Firebase.

I am creating a Android app that stores data like name, number to SQLite database. I need to push the data from SQLite to Firebase.

这是用于将数据存储在 detailsdb

This is the SQLite code for the app which stores the data in detailsdb

sqLiteHelper = new SQLiteHelper(this, "DetailsDB.sqlite", null, 1);

    sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS DETAILS(Id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, phone VARCHAR, location VARCHAR)");
onClick save

            try {
                sqLiteHelper.insertData(
                        eName.getText().toString().trim(),
                        ePhonenumber.getText().toString().trim(),
                        eLocation.getText().toString().trim()
                );

                Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show();
                eName.setText("");
                ePhonenumber.setText("");
                eLocation.setText("");
            } catch (Exception e) {
                e.printStackTrace();
            }

我需要从此处的 detailsdb.sqlite 同步或插入Firebase数据库中

I need to sync or insert into Firebase database from detailsdb.sqlite here

 if(isOnline(MainActivity.this))

    {
        Toast.makeText(getApplicationContext(), "Internet is Available", Toast.LENGTH_LONG).show();

        //Read SQlite db and sync/Store them to firebase.


    }

推荐答案

创建一个如下所示的类:

Create A class Like Below:

class Details{
   public String eName,ePhonenumber,eLocation;
   public Details(String name,String number,String location){
        this.eName = name;
        this.ePhonenumber = number;
        this.eLocation = location;
   }
}

使用这样的查询从sqlite获取数据:

use the query like this to fetch data from sqlite:

List<Details> dataList = new ArrayList<Details>;
Cursor c = sqLiteHelper.rawQuery("select * from DETAILS",null);
if (cursor.moveToFirst()) {
    do {
        dataList.add(new Details(cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getString(cursor.getColumnIndex("phone")),
                    cursor.getString(cursor.getColumnIndex("location"))))
    } while (cursor.moveToNext());
}

您可以像这样发送到Firebase

and you can send to firebase like this

if(dataList.size() > 0 ){
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("DETAILS");
    for(Details d : dataList){
        ref.push().setValue(d);
    }
}

这篇关于如何将数据从SQLite数据库发送到Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:31