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

时间:2017-11-09 10:05:29

标签: android sqlite firebase firebase-realtime-database

我正在创建一个Android应用程序,它将名称,数字等数据存储到SQLite数据库中。我需要将数据从SQLite推送到Firebase。

这是应用程序的SQLite代码,用于将数据存储在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数据库
 if(isOnline(MainActivity.this))

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

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


    }

1 个答案:

答案 0 :(得分:0)

创建类似下面的类:

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获取数据:

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

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