gpt4 book ai didi

firebase - 如何将数据从 Firebase 保存到 SQLite Android

转载 作者:行者123 更新时间:2023-12-02 12:40:58 25 4
gpt4 key购买 nike

我正在从 Firebase 中获取一些数据,并尝试使用相同的 onDataChange 方法 将数据保存到 SQLite 数据库。下面是我迄今为止尝试过的代码。我正在将获取的数据保存在 String 变量中。当我尝试使用 Toast 时,它会显示获取的值,并且当我对变量使用硬编码值(如 String drivername="some value")时,它也会显示获取的值>它正在将数据保存到sqlite,但是当我尝试保存获取的值时,它返回空值,因此不保存。我做错了什么

    String mobile_no=localSharedPreferences.getSharedPreferences(Constants.PhoneNumber);

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Driver");

reference.orderByKey().equalTo(mobile_no).addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

String drivermobile=localSharedPreferences.getSharedPreferences(Constants.PhoneNumber);

for(DataSnapshot datas: dataSnapshot.getChildren()){

String drivername = datas.child("d_nme").getValue(String.class);
String avatorurl = datas.child("d_avtr").getValue(String.class);
String vehicleno = datas.child("v_No").getValue(String.class);
String vehiclename = datas.child("v_nme").getValue(String.class);

//Toast is showing fetched drivername correctly
Toast.makeText(SigninActivity.this, drivername, Toast.LENGTH_SHORT).show();

//Trying to save into SQLITE

ContactsDB db = new ContactsDB(getApplicationContext());
db.open();
db.createEntry(drivermobile, drivername, vehiclename, avatorurl, vehicleno);
db.close();


Toast.makeText(SigninActivity.this, localSharedPreferences.getSharedPreferences(Constants.dDrivername), Toast.LENGTH_SHORT).show();
}

这是我的 ContactsDB 类

public class ContactsDB {

public static final String KEY_ROWID="_id";
public static final String DRIVER_MOBILE="driver_mobile";
public static final String DRIVER_NAME="driver_firstname";
public static final String VEHICLE_NAME="vehicle_name";
public static final String AVATOR_URL="driver_avator";
public static final String VEHICLE_NO="vehicle_no";

private final String DATABASE_NAME="ContactsDB";
private final String DATABASE_TABLE="ContactsTable";
private final int DATABASE_VERSION=1;

private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabse;

public ContactsDB(Context context)
{
ourContext=context;
}

private class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(sqLiteDatabase);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

/**
* THIS Only runs if database not exists with specified version
*/

String sqlcode="CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
DRIVER_MOBILE + " INTEGER NOT NULL, " +
DRIVER_NAME + " TEXT NOT NULL, " +
VEHICLE_NAME + " TEXT NOT NULL, " +
AVATOR_URL + " TEXT NOT NULL, " +
VEHICLE_NO + " DECIMAL NOT NULL);";

sqLiteDatabase.execSQL(sqlcode);

}
}


public ContactsDB open() throws SQLException
{
ourHelper=new DBHelper(ourContext);
ourDatabse=ourHelper.getWritableDatabase();
return this;
}

public void close()
{
ourHelper.close();
}

public long createEntry(String drivermobile, String drivername, String vehiclename, String avatorurl, String vehicleno) {


ContentValues cv = new ContentValues();
cv.put(DRIVER_MOBILE, drivermobile);
cv.put(DRIVER_NAME,drivername);
cv.put(VEHICLE_NAME,vehiclename);
cv.put(AVATOR_URL,avatorurl);
cv.put(VEHICLE_NO,vehicleno);
return ourDatabse.insert(DATABASE_TABLE, null, cv);

}

public String getData()
{
/*Here you can Specify which column value you want to retrieve*/

String [] columns=new String[] {KEY_ROWID,DRIVER_MOBILE,DRIVER_NAME,VEHICLE_NAME,AVATOR_URL,VEHICLE_NO};
Cursor c=ourDatabse.query(DATABASE_TABLE, columns, null,null,null,null,null);

String result="";

int iKeyID=c.getColumnIndex(KEY_ROWID);
int iRowID=c.getColumnIndex(DRIVER_MOBILE);
int iFirstName=c.getColumnIndex(DRIVER_NAME);
int iSecondName=c.getColumnIndex(VEHICLE_NAME);
int iAvatorUrl=c.getColumnIndex(AVATOR_URL);
int iRatings=c.getColumnIndex(VEHICLE_NO);

for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{

result=result + c.getString(iKeyID) + ": " + c.getString(iRowID) + ": " + c.getString(iFirstName) + ": " +
c.getString(iSecondName) + ": " + c.getString(iAvatorUrl) + "+ " +
c.getString(iRatings) + "\n"; // n is for newline
}
c.close();
return result;

}


public long deleteEntry(String rowId)
{
return ourDatabse.delete(DATABASE_TABLE, KEY_ROWID + "=?", new String[]{rowId});
}


public long updateEntry(String rowId, String drivermobile, String drivername, String vehiclename, String avatorurl, String vehicleno)
{
ContentValues cv=new ContentValues();

/*if you update one column then put only one column or put as many as many you want to update*/
cv.put(DRIVER_MOBILE, drivermobile);
cv.put(DRIVER_NAME,drivername);
cv.put(VEHICLE_NAME,vehiclename);
cv.put(AVATOR_URL,avatorurl);
cv.put(VEHICLE_NO,vehicleno);
return ourDatabse.update(DATABASE_TABLE, cv, KEY_ROWID + "=?", new String[]{rowId});
}

最佳答案

我建议您尝试以下操作:-

1.

将以下 getter 方法添加到 ContactsDB.java :-

public SQLiteDatabase getOurDatabse() {
return ourDatabse;
}

2

openclose 分别移至循环之前和之后,并在循环之前和循环之后添加 beginTransaction < strong>setTransactionSuccessful 和 endTransaction

因此,磁盘将只写入一次,而不是每次插入。

例如:-

public void onDataChange(DataSnapshot dataSnapshot) {

ContactsDB db = new ContactsDB(getApplicationContext());
db.open();
db.getOurDatabse().beginTransaction();


String drivermobile = localSharedPreferences.getSharedPreferences(Constants.PhoneNumber);

for (DataSnapshot datas : dataSnapshot.getChildren()) {


String drivername = datas.child("d_nme").getValue(String.class);
String avatorurl = datas.child("d_avtr").getValue(String.class);
String vehicleno = datas.child("v_No").getValue(String.class);
String vehiclename = datas.child("v_nme").getValue(String.class);

//Toast is showing fetched drivername correctly
Toast.makeText(SigninActivity.this, drivername, Toast.LENGTH_SHORT).show();

//Trying to save into SQLITE

if (drivermobile != null && drivername != null && vehiclename != null && avatorurl != null && vehicleno != null) {
if (db.createEntry(drivermobile, drivername, vehiclename, avatorurl, vehicleno) > 0) {
Toast.makeText(SigninActivity.this, localSharedPreferences.getSharedPreferences(Constants.dDrivername), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SigninActivity.this, "?????? Not ADDED", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(SigninActivity.this, "?????? Unable to ADD due to NULL value(s)", Toast.LENGTH_SHORT).show();
}

}
db.getOurDatabse().setTransactionSuccessful();
db.getOurDatabse().endTransaction();
db.close();
  • 这还会进行一些检查,以便空值不会导致尝试插入,但会提示无法添加行。
  • 它还会检查每次插入的返回值,以查看该行是否已插入以及 Toast 是否已插入。
    • 但是,我建议将消息​​写入日志而不是(或同时)Toast 将有助于调试。

关于firebase - 如何将数据从 Firebase 保存到 SQLite Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com