保存列表<模型类>到sqlite [英] save List&lt;Model class&gt; to sqlite

查看:56
本文介绍了保存列表<模型类>到sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Firebase 应用,我想将包含 5000 多个检索到的 PricesList 项目的列表保存到 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 中,您需要一种将数据插入数据库的方法,以便..第一:创建方法

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.

第二:我们需要一个助手实例并调用新方法,下一行进入您的迭代.

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

这篇关于保存列表<模型类>到sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆