问题
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