Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

android - save List<Model class> to sqlite

I'm working on the Firebase app and I want to save a list of more than 5000 retrieved PricesList items into SQLite.

Here is my Firebase code:

@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) {

        }
    });
}

and here is the DB.class code

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 );
    }
}

What code should be used for storing listo of items in SQLite database? I'll be thankful to anyone how has the answer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...