How to send data from SQLite database to Firebase database

£可爱£侵袭症+ 提交于 2021-02-08 12:09:45

问题


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.

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();
            }

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.


    }

回答1:


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;
   }
}

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());
}

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);
    }
}


来源:https://stackoverflow.com/questions/47198959/how-to-send-data-from-sqlite-database-to-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!