android sqlite 数据库简单封装示例(java)

打印 上一主题 下一主题

主题 865|帖子 865|积分 2595

sqlite 数据库简单封装示例,使用记事本数据库表举行示例。
首先继续SQLiteOpenHelper 使用sql语句举行创建一张表。
  1. public class noteDBHelper extends SQLiteOpenHelper {
  2.     public noteDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  3.         super(context, name, factory, version);
  4.     }
  5.     @Override
  6.     public void onCreate(SQLiteDatabase sqLiteDatabase) {
  7.         String sql="create table if not exists note_data(" +
  8.                 "note_id integer primary key autoincrement," +
  9.                 "note_tittle varchar,"+
  10.                 "note_content varchar,"+
  11.                 "note_type varchar,"+
  12.                 "createTime varchar,"+
  13.                 "updateTime varchar,"+
  14.                 "note_owner varchar)";
  15.         sqLiteDatabase.execSQL(sql);
  16.     }
  17.     @Override
  18.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  19.     }
  20. }
复制代码
使用单例封装这张表的增删改查,同时转换成字段对应的布局体。这样方便数据管理,插入,查询,更新等操作。 
  1. public class NoteDaoManage {
  2.     private static NoteDaoManage noteDaoManage;
  3.     Context context;
  4.     noteDBHelper dbHelper;
  5.     public static NoteDaoManage GetInstance(Context base) {
  6.         if (noteDaoManage == null) {
  7.             noteDaoManage = new NoteDaoManage(base);
  8.         }
  9.         return noteDaoManage;
  10.     }
  11.     private NoteDaoManage(Context context) {
  12.         this.context = context;
  13.         dbHelper = new noteDBHelper(context, "note.db", null, 1);
  14.     }
  15.     public void insertNote(NoteBean bean){
  16.         SQLiteDatabase sqLiteDatabase= dbHelper.getWritableDatabase();
  17.         ContentValues cv = new ContentValues();
  18.         cv.put("note_tittle",bean.getTitle());
  19.         cv.put("note_content",bean.getContent());
  20.         cv.put("note_type",bean.getType());
  21.         cv.put("createTime",bean.getCreateTime());
  22.         cv.put("updateTime",bean.getUpdateTime());
  23.         cv.put("note_owner",bean.getOwner());
  24.         sqLiteDatabase.insert("note_data",null,cv);
  25.     }
  26.     public int DeleteNote(int id){
  27.         SQLiteDatabase sqLiteDatabase= dbHelper.getWritableDatabase();
  28.         int ret=0;
  29.         ret=sqLiteDatabase.delete("note_data","note_id=?",new String[]{id + ""});
  30.         return ret;
  31.     }
  32.     @SuppressLint("Range")
  33.     public  List<NoteBean>  getAllData(){
  34.         List<NoteBean> noteList = new ArrayList<>();
  35.         SQLiteDatabase db = dbHelper.getWritableDatabase();
  36.         String sql="select * from note_data "; // 查询全部数据
  37.         Cursor cursor = db.rawQuery(sql,null);
  38.         while (cursor.moveToNext()) {
  39.             NoteBean note = new NoteBean();
  40.             note.setId(cursor.getInt(cursor.getColumnIndex("note_id")));
  41.             note.setTitle(cursor.getString(cursor.getColumnIndex("note_tittle")));
  42.             note.setContent(cursor.getString(cursor.getColumnIndex("note_content")));
  43.             note.setType(cursor.getString(cursor.getColumnIndex("note_type")));
  44.             note.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
  45.             note.setUpdateTime(cursor.getString(cursor.getColumnIndex("updateTime")));
  46.             noteList.add(note);
  47.         }
  48.         if (cursor != null) {
  49.             cursor.close();
  50.         }
  51.         if (db != null) {
  52.             db.close();
  53.         }
  54.         return noteList;
  55.     }
  56.     public void updateNote(NoteBean note) {
  57.         SQLiteDatabase db = dbHelper.getWritableDatabase();
  58.         ContentValues cv = new ContentValues();
  59.         cv.put("note_tittle", note.getTitle());
  60.         cv.put("note_content", note.getContent());
  61.         cv.put("note_type", note.getType());
  62.         cv.put("updateTime", note.getUpdateTime());
  63.         db.update("note_data", cv, "note_id=?", new String[]{note.getId()+""});
  64.         db.close();
  65.     }
  66.     @SuppressLint("Range")
  67.     public List<NoteBean> queryNotesAll(int mark, String text) {
  68.         SQLiteDatabase db = dbHelper.getWritableDatabase();
  69.         List<NoteBean> noteList = new ArrayList<>();
  70.         NoteBean note;
  71.         String sql ;
  72.         Cursor cursor = null;
  73.         if (TextUtils.isEmpty(text)){
  74.             sql="select * from note_data "; // 查询全部数据
  75.         }else {
  76.             if(mark==0){
  77.                 sql = "SELECT * FROM note_data WHERE note_tittle LIKE '%" + text + "%'" + " order by note_id desc"; // 构建SQL语句
  78.             }else {
  79.                 sql = "SELECT * FROM note_data WHERE note_content LIKE '%" + text + "%'" + " order by note_id desc"; // 构建SQL语句
  80.             }
  81.         }
  82.         cursor = db.rawQuery(sql,null);
  83.         while (cursor.moveToNext()) {
  84.             note = new NoteBean();
  85.             note.setId(cursor.getInt(cursor.getColumnIndex("note_id")));
  86.             note.setTitle(cursor.getString(cursor.getColumnIndex("note_tittle")));
  87.             note.setContent(cursor.getString(cursor.getColumnIndex("note_content")));
  88.             note.setType(cursor.getString(cursor.getColumnIndex("note_type")));
  89.             note.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
  90.             note.setUpdateTime(cursor.getString(cursor.getColumnIndex("updateTime")));
  91.             noteList.add(note);
  92.             }
  93.         if (cursor != null) {
  94.             cursor.close();
  95.         }
  96.         if (db != null) {
  97.             db.close();
  98.         }
  99.         return noteList;
  100.     }
  101. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

欢乐狗

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表