赞
踩
- public class DatabaseHelper extends SQLiteOpenHelper {
-
- private static final String TABLE_NAME = "Table_data";
-
- public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- String sql = "create table if not exists " + TABLE_NAME + " (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT, packageName text ,className text, appName text, isPermanent integer,drawableId integer,resolveInfo BLOB)";
- db.execSQL(sql);
- }
-
-
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
- db.execSQL(sql);
- onCreate(db);
- }
- }
- public class DatabaseManager {
- private final DatabaseHelper databaseHelper;
- private static final String TABLE_NAME = "Table_data";
-
- public DatabaseManager(Context context) {
- databaseHelper = new DatabaseHelper(context, "demo_db", null, 1);
- }
-
- public void insert(ContentValues contentValues) {
- SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
- writableDatabase.insert(TABLE_NAME, null, contentValues);
- writableDatabase.close();
- }
-
-
- public void update(ContentValues contentValues) {
- SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
- writableDatabase.update(TABLE_NAME, contentValues, "id=?", new String[1]);
- writableDatabase.close();
- }
-
- public void query() {
- SQLiteDatabase readableDatabase = databaseHelper.getReadableDatabase();
- Cursor cursor = readableDatabase.query(TABLE_NAME, null, "id=?", new String[]{"1"}, null, null, null);
- while (cursor.moveToNext()) {
- String id = cursor.getString(cursor.getColumnIndex("id"));
- }
- readableDatabase.close();
- }
-
- public void delete(String whereClause, String[] whereArgs) {
- SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
- writableDatabase.delete(TABLE_NAME, whereClause, whereArgs);
- writableDatabase.close();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。