保存List< Model类>到sqlite [英] save List<Model class> to sqlite
问题描述
我正在使用Firebase应用程序,我想将超过5000个检索到的PriceList项目列表保存到SQLite中.
I'm working on the Firebase app and I want to save a list of more than 5000 retrieved PricesList items into SQLite.
这是我的 Firebase代码:
@Override
protected void onStart() {
super.onStart();
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pricesArrayList.clear();
for (DataSnapshot PricesListDataSnapshot : dataSnapshot.getChildren()) {
PricesList pricesList = PricesListDataSnapshot.getValue(PricesList.class);
pricesArrayList.add(pricesList);
// I don't know what is the insertion code for SQLite
}
// here goes the code for retrieved data from SQLite after it was saved from Firebase
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是 DB.class 代码
private class DBHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DB_NAME = "MyDB1.db";
public static final String id = "_id";
public static final String ItemCode = "ItemCode";
public static final String Product = "Product";
DBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String create_sql = "CREATE TABLE IF NOT EXISTS "
+ DB_NAME + '(' + id
+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ItemCode + " TEXT ,"
+ Product + " TEXT ," +')';
db.execSQL(create_sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DB_NAME );
}
}
应使用什么代码在SQLite数据库中存储项目列表?我将感谢任何有此答案的人.
What code should be used for storing listo of items in SQLite database? I'll be thankful to anyone how has the answer.
推荐答案
在您的DBHelper中,您需要一个将数据插入db中的方法.首先:创建方法
In your DBHelper you need a method that insert your data in db so.. first: Create the method
public void isInsertData(Price price) {
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put(ItemCode, price.getItemCode());
insertValues.put(Product, price.getProduct());
db.insert(DB_NAME, null, insertValues);
} catch (Exception e) {
e.printStackTrace();
}
}
我看到您的表名看起来与您的数据库名相似,我建议您更改它,但是如果需要的话.
I see that your table name looks similar to your database name, i recommend you change that but its if you want.
第二:我们需要一个帮助程序实例,并调用new方法,下一行进入您的迭代.
Second: we need a instance of our helper and call the new method, the next line goes in your iteration.
DbHelper dbHelper = new DbHelper(this); //or ActivityName.this
for (DataSnapshot PricesListDataSnapshot : dataSnapshot.getChildren()) {
PricesList pricesList = PricesListDataSnapshot.getValue(PricesList.class);
pricesArrayList.add(pricesList);
dbHelper.isInsertData(pricesList);
}
就是这样!现在,您将数据保存在数据库中.
That´s it! Now you save data in your database.
如果之后有任何疑问,建议您阅读此链接 https://developer.android.com/training/data-storage/sqlite
I recommend you read this link if you have any question after that https://developer.android.com/training/data-storage/sqlite
这篇关于保存List< Model类>到sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!