菜鸟进阶笔记|SQLite|01|SQLite二次封装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yang96515328/article/details/71629661

经过了一天的时间,不断的修改,终于跑通它啦,写到这里记下来供自已以后使用~

1.首先新建 一个Bean:

public class ExamInfo {
    public static int id;
    public static String name;
    public static String examid;
    public static String filepath;

    public ExamInfo() {
    }

    public ExamInfo(String name, String examid, String filepath) {
        this.name = name;
        this.examid = examid;
        this.filepath = filepath;
    }

    @Override
    public String toString() {
        return "ExamInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", examid=" + examid +
                ", filepath=" + filepath +
                '}';
    }

2.然后新建一个用于封装SQLite的工具类:

public class TreeDBDao {
    private static final String DB_NAME = "sgox";//数据库名称
    private static final String TABLE_NAME = "exam";//数据表名称
    private static final int DB_VERSION = 1;//数据库版本

    //表的字段名
    private static String KEY_ID = "id";
    private static String KEY_NAME = "name";
    private static String KEY_EXAMID = "examid";
    private static String KEY_FILEPATH = "filepath";

    private SQLiteDatabase mDatabase;
    private Context mContext;
    private TreeDBOpenHelper mDbOpenHelper;//数据库打开帮助类


    public TreeDBDao(Context context) {
        mContext = context;
    }

    //打开数据库
    public void openDataBase() {
        mDbOpenHelper = new TreeDBOpenHelper(mContext, DB_NAME, null, DB_VERSION);
        try {
            mDatabase = mDbOpenHelper.getWritableDatabase();//获取可写数据库
        } catch (SQLException e) {
            mDatabase = mDbOpenHelper.getReadableDatabase();//获取只读数据库
        }
    }

    //关闭数据库
    public void closeDataBase() {
        if (mDatabase != null) {
            mDatabase.close();
        }
    }

    //插入一条数据
    public long insertData(ExamInfo ei) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, ei.name);
        values.put(KEY_EXAMID, ei.examid);
        values.put(KEY_FILEPATH, ei.filepath);
        return mDatabase.insert(TABLE_NAME, null, values);
    }

    //删除一条数据
    public long deleteData(long id) {
        return mDatabase.delete(TABLE_NAME, KEY_ID + "=" + id, null);
    }

    //删除所有数据
    public long deleteAllData() {
        return mDatabase.delete(TABLE_NAME, null, null);
    }

    //更新一条数据
    public long updateData(long id, ExamInfo examInfo) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, examInfo.name);
        values.put(KEY_EXAMID, examInfo.examid);
        values.put(KEY_FILEPATH, examInfo.filepath);
        return mDatabase.update(TABLE_NAME, values, KEY_ID + "=" + id, null);
    }

    //查询一条数据
    public List<ExamInfo> queryData(long id, String name) {
        Cursor results = mDatabase.query(TABLE_NAME, new String[]{KEY_ID, KEY_NAME, KEY_EXAMID, KEY_FILEPATH},
                KEY_ID + "=" + id + " and " + KEY_NAME + "='" + name + "'", null, null, null, null);
        return convertToTree(results);
    }

    //查询所有数据
    public List<ExamInfo> queryDataList() {
        Cursor results = mDatabase.query(TABLE_NAME, new String[]{KEY_ID, KEY_NAME, KEY_EXAMID, KEY_FILEPATH},
                null, null, null, null, null);
        return convertToTree(results);

    }

    //查询某条数据是否存在
    public boolean isTrue(String a) {
        Cursor cursor = mDatabase.query(TABLE_NAME, null, KEY_NAME + " = '" + a + "'",
                null, null, null, null);
        if (cursor.moveToNext())
            return true;
        else
            return false;
    }

    private List<ExamInfo> convertToTree(Cursor cursor) {
        int resultCounts = cursor.getCount();
        if (resultCounts == 0 || !cursor.moveToFirst()) {
            return null;
        }
        List<ExamInfo> mTreeList = new ArrayList<>();
        for (int i = 0; i < resultCounts; i++) {
            ExamInfo examInfo = new ExamInfo();
            examInfo.id = cursor.getInt(0);
            examInfo.name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
            examInfo.examid = cursor.getString(cursor.getColumnIndex(KEY_EXAMID));
            examInfo.filepath = cursor.getString(cursor.getColumnIndex(KEY_FILEPATH));
            mTreeList.add(examInfo);
            cursor.moveToNext();
        }
        return mTreeList;
    }

    /**
     * 数据表打开帮助类
     */
    private static class TreeDBOpenHelper extends SQLiteOpenHelper {

        public TreeDBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            final String sqlStr = "create table if not exists " + TABLE_NAME + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME + " text not null, " + KEY_EXAMID + " text not null," + KEY_FILEPATH + " text not null);";
            db.execSQL(sqlStr);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            final String sqlStr = "DROP TABLE IF EXISTS " + TABLE_NAME;
            db.execSQL(sqlStr);
            onCreate(db);
        }
    }
}

3.使用这个工具类:

TreeDBDao mDBDao = new TreeDBDao(MainActivity.this);//实例化对象

mDBDao.openDataBase();//打开数据库

boolean a = mDBDao.isTrue("二狗");//查询该数据是否存在

mDBDao.insertData(new Tree("小明", "编号", "对应地址"));//增加数据

mDBDao.deleteData(1, "小明");//删除id为1并且姓名为小明的数据

mDBDao.deleteAllData();//删除所有数据

mDBDao.updateData(1, new ExamInfo("小明的女朋友", "新编号", "新的对应地址"));//更新数据

List<ExamInfo> list = mDBDao.queryData(1, "小明");//查询id为1并且姓名为小明的数据
Log.e("-->", list.get(0).toString());

List<ExamInfo> lists = mDBDao.queryDataList();//查询所有数据
for (Tree tree : lists) {
     Log.e("-->", tree.toString());
}

好了,这几个方法可以满足最基本的需求啦!~

阅读更多
换一批

没有更多推荐了,返回首页