How to add initial data to SQLite database?

后端 未结 6 1647
逝去的感伤
逝去的感伤 2021-02-14 07:04

I need to add initial values to SQLite database, once application is started first time. How should I do it?

6条回答
  •  旧巷少年郎
    2021-02-14 07:44

    You should use SQLiteOpenHelper for this

        private static class OpenHelper extends SQLiteOpenHelper {
    
        private static final String DATABASE_NAME = "rss.sqlite";
        private static final int DATABASE_VERSION = 1;
    
        OpenHelper(Context context) {
           super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE RSS (TITLE TEXT, LINK TEXT, DESCR TEXT, PUBDATE DATE, GUID TEXT, READ TEXT, TYPE TEXT)");
            db.execSQL("CREATE TABLE PAGE (LINK TEXT, CONTENT TEXT)");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    
        }
    }
    

    And use it in code

        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
    

提交回复
热议问题