当前位置:   article > 正文

SQLiteDatabase基本使用_writabledatabase.insert

writabledatabase.insert
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2. private static final String TABLE_NAME = "Table_data";
  3. public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  4. super(context, name, factory, version);
  5. }
  6. @Override
  7. public void onCreate(SQLiteDatabase db) {
  8. 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)";
  9. db.execSQL(sql);
  10. }
  11. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  12. String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
  13. db.execSQL(sql);
  14. onCreate(db);
  15. }
  16. }
  1. public class DatabaseManager {
  2. private final DatabaseHelper databaseHelper;
  3. private static final String TABLE_NAME = "Table_data";
  4. public DatabaseManager(Context context) {
  5. databaseHelper = new DatabaseHelper(context, "demo_db", null, 1);
  6. }
  7. public void insert(ContentValues contentValues) {
  8. SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
  9. writableDatabase.insert(TABLE_NAME, null, contentValues);
  10. writableDatabase.close();
  11. }
  12. public void update(ContentValues contentValues) {
  13. SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
  14. writableDatabase.update(TABLE_NAME, contentValues, "id=?", new String[1]);
  15. writableDatabase.close();
  16. }
  17. public void query() {
  18. SQLiteDatabase readableDatabase = databaseHelper.getReadableDatabase();
  19. Cursor cursor = readableDatabase.query(TABLE_NAME, null, "id=?", new String[]{"1"}, null, null, null);
  20. while (cursor.moveToNext()) {
  21. String id = cursor.getString(cursor.getColumnIndex("id"));
  22. }
  23. readableDatabase.close();
  24. }
  25. public void delete(String whereClause, String[] whereArgs) {
  26. SQLiteDatabase writableDatabase = databaseHelper.getWritableDatabase();
  27. writableDatabase.delete(TABLE_NAME, whereClause, whereArgs);
  28. writableDatabase.close();
  29. }
  30. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/636323
推荐阅读
  

闽ICP备14008679号