首先,创建DbHelper对象,继承SQLiteOpenHelper。
Configuration是自行创建的工具类,里面都是App的一些环境设置。
public class DbHelper extends SQLiteOpenHelper { private Configuration config = new Configuration(); private static DbHelper Instance = null; private Context context; // 饿汉式 public static DbHelper getInstance(Context context) { if (Instance == null) { Instance = new DbHelper(context.getApplicationContext()); } return Instance; } /* * @param [Context]context,来自DAO的上下文图 */ private DbHelper(Context context) { super(context, Configuration.DB_NAME, null, Configuration.DB_VERSION); this.context = context; } public void onCreate(SQLiteDatabase db) { db.execSQL(config.CREATE_USER_TABLE); db.execSQL(config.CREATE_POST_TABLE); } /* * call this method if you need to update SQlite but I really don't suggest to use this */ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(" DROP TABLE IF EXISTS "+config.DB_USER); db.execSQL(" DROP TABLE IF EXISTS "+config.DB_POST); } }