Android Sqlite的使用,纯代码

SQLiteOpenHelper

/***
 * 作者 : 于德海
 * 时间 : 2019/8/27 0027 11:28
 * 描述 :
 */
public class SQLiteDbHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "cyber_database";
    public static final int DB_VERSION = 1;



    public SQLiteDbHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CollectionDbManager.TABLE_COLLECTION_CREATE_SQL);
        db.execSQL(HistoryDbManager.TABLE_HISTORY_CREATE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

DB Bean

/***
 * 作者 : 于德海
 * 时间 : 2019/8/27 0027 11:40
 * 描述 : 收藏数据库类
 */
public class CollectionDbBean {
    private String contentCode;
    private int startTimes;
    private String contentName;
    private  String posterUrl;
    private String lastUsedTime;
    private String startCMD;
    private int runMode;
    private int isVip;
    private int isYearVip;
    private int ischecked;
    private boolean isdelete;

    public String getContentCode() {
        return contentCode;
    }

    public void setContentCode(String contentCode) {
        this.contentCode = contentCode;
    }

    public int getStartTimes() {
        return startTimes;
    }

    public void setStartTimes(int startTimes) {
        this.startTimes = startTimes;
    }

    public String getContentName() {
        return contentName;
    }

    public void setContentName(String contentName) {
        this.contentName = contentName;
    }

    public String getPosterUrl() {
        return posterUrl;
    }

    public void setPosterUrl(String posterUrl) {
        this.posterUrl = posterUrl;
    }

    public String getLastUsedTime() {
        return lastUsedTime;
    }

    public void setLastUsedTime(String lastUsedTime) {
        this.lastUsedTime = lastUsedTime;
    }

    public String getStartCMD() {
        return startCMD;
    }

    public void setStartCMD(String startCMD) {
        this.startCMD = startCMD;
    }

    public int getRunMode() {
        return runMode;
    }

    public void setRunMode(int runMode) {
        this.runMode = runMode;
    }

    public int getIsVip() {
        return isVip;
    }

    public void setIsVip(int isVip) {
        this.isVip = isVip;
    }

    public int getIsYearVip() {
        return isYearVip;
    }

    public void setIsYearVip(int isYearVip) {
        this.isYearVip = isYearVip;
    }

    public int getIschecked() {
        return ischecked;
    }

    public void setIschecked(int ischecked) {
        this.ischecked = ischecked;
    }

    public boolean isIsdelete() {
        return isdelete;
    }

    public void setIsdelete(boolean isdelete) {
        this.isdelete = isdelete;
    }
}
public class HistoryDbBean {
    /**
     * contentCode : 70001008
     * contentName : 全民潜艇
     * posterUrl : null
     * startCMD : apk:com.xxz.QianTing
     * runMode : 0
     * androidAPKInfo : null
     * startTimes : 24
     * onlineTime : 2019-03-20 16:14:38
     */

    private int isSelected;
    private String contentCode;
    private String contentName;
    private String posterUrl;
    private String startCMD;
    private String lastUsedTime;
    private int runMode;
    private Object androidAPKInfo;
    private int startTimes;
    private String onlineTime;
    private int isVip;
    private int isYearVip;

    public int getIsYearVip() {
        return isYearVip;
    }

    public void setIsYearVip(int isYearVip) {
        this.isYearVip = isYearVip;
    }

    public int getIsVip() {
        return isVip;
    }

    public void setIsVip(int isVip) {
        this.isVip = isVip;
    }

    public int getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(int isSelected) {
        this.isSelected = isSelected;
    }

    public String getLastUsedTime() {
        return lastUsedTime;
    }

    public void setLastUsedTime(String lastUsedTime) {
        this.lastUsedTime = lastUsedTime;
    }

    public String getContentCode() {
        return contentCode;
    }

    public void setContentCode(String contentCode) {
        this.contentCode = contentCode;
    }

    public String getContentName() {
        return contentName;
    }

    public void setContentName(String contentName) {
        this.contentName = contentName;
    }

    public String getPosterUrl() {
        return posterUrl;
    }

    public void setPosterUrl(String posterUrl) {
        this.posterUrl = posterUrl;
    }

    public String getStartCMD() {
        return startCMD;
    }

    public void setStartCMD(String startCMD) {
        this.startCMD = startCMD;
    }

    public int getRunMode() {
        return runMode;
    }

    public void setRunMode(int runMode) {
        this.runMode = runMode;
    }

    public Object getAndroidAPKInfo() {
        return androidAPKInfo;
    }

    public void setAndroidAPKInfo(Object androidAPKInfo) {
        this.androidAPKInfo = androidAPKInfo;
    }

    public int getStartTimes() {
        return startTimes;
    }

    public void setStartTimes(int startTimes) {
        this.startTimes = startTimes;
    }

    public String getOnlineTime() {
        return onlineTime;
    }

    public void setOnlineTime(String onlineTime) {
        this.onlineTime = onlineTime;
    }
}

CollectionDbManager

/***
 * 作者 : 于德海
 * 时间 : 2019/8/27 0027 11:41
 * 描述 : 收藏数据库管理类
 */
public class CollectionDbManager {
    public static final String TABLE_COLLECTION = "collection";
    public static final String TABLE_COLLECTION_CREATE_SQL = "create table "+TABLE_COLLECTION+
            " (_id integer primary key autoincrement,"+
            " app_id integer,"+
            " user_id text,"+
            " app_name text,"+
            " img_url text,"
            +" use_people integer,"+
            " is_vip integer,"+
            " is_year_vip integer);";

    private SQLiteDatabase mSQLiteDatabase;
    public CollectionDbManager(Context context) {
        SQLiteDbHelper helper = new SQLiteDbHelper(context.getApplicationContext());
        mSQLiteDatabase = helper.getWritableDatabase();
    }




    public void insertValue(List<CollectionDbBean> list){
        if(list == null)
            return;
        for (CollectionDbBean bean : list)
            insertValue(bean);
    }

    public void insertValue(CollectionDbBean bean){
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id",bean.getContentCode());
        contentValues.put("user_id",AppConfig.UserID);
        contentValues.put("app_name",bean.getContentName());
        contentValues.put("img_url",bean.getPosterUrl());
        contentValues.put("use_people",bean.getStartTimes());
        contentValues.put("is_vip",bean.getIsVip());
        contentValues.put("is_year_vip",bean.getIsYearVip());
        mSQLiteDatabase.insert(TABLE_COLLECTION,null,contentValues);
    }


    public List<CollectionDbBean> queryCollection(){
        List<CollectionDbBean> list = new ArrayList<>();
        Cursor cursor = mSQLiteDatabase.query(TABLE_COLLECTION,null,"user_id=?",new String[]{AppConfig.UserID},null,null,null);
        while (cursor.moveToNext()){
            CollectionDbBean bean = new CollectionDbBean();
            bean.setContentCode(cursor.getString(cursor.getColumnIndex("app_id")));
            bean.setContentName(cursor.getString(cursor.getColumnIndex("app_name")));
            bean.setPosterUrl(cursor.getString(cursor.getColumnIndex("img_url")));
            bean.setStartTimes(cursor.getInt(cursor.getColumnIndex("use_people")));
            bean.setIsVip(cursor.getInt(cursor.getColumnIndex("is_vip")));
            bean.setIsYearVip(cursor.getInt(cursor.getColumnIndex("is_year_vip")));
            list.add(bean);
        }
        cursor.close();
        return list;
    }

    public CollectionDbBean queryCollection(String contentCode){
        Cursor cursor = mSQLiteDatabase.query(TABLE_COLLECTION,null,"user_id = ? and app_id = ?",new String[]{AppConfig.UserID,contentCode},null,null,null);
        while (cursor.moveToNext()){
            CollectionDbBean bean = new CollectionDbBean();
            bean.setContentCode(cursor.getString(cursor.getColumnIndex("app_id")));
            bean.setContentName(cursor.getString(cursor.getColumnIndex("app_name")));
            bean.setPosterUrl(cursor.getString(cursor.getColumnIndex("img_url")));
            bean.setStartTimes(cursor.getInt(cursor.getColumnIndex("use_people")));
            bean.setIsVip(cursor.getInt(cursor.getColumnIndex("is_vip")));
            bean.setIsYearVip(cursor.getInt(cursor.getColumnIndex("is_year_vip")));
            cursor.close();
            return bean;
        }
        cursor.close();
        return null;
    }

    public void delete(String contentCode){
        if(TextUtils.isEmpty(AppConfig.UserID))
            return;
        mSQLiteDatabase.delete(TABLE_COLLECTION,"user_id = ? and app_id = ?",new String[]{AppConfig.UserID,contentCode});
    }



}

HistoryDbManager

/***
 * 作者 : 于德海
 * 时间 : 2019/8/27 0027 11:41
 * 描述 : 历史记录数据库管理类
 */
public class HistoryDbManager {
    private static final String TABLE_HISTORY = "history";
    public static final String TABLE_HISTORY_CREATE_SQL = "create table "+TABLE_HISTORY+
            " (_id integer primary key autoincrement,"+
            " app_id integer,"+
            " user_id text,"+
            " app_name text,"+
            " use_time text,"+
            " img_url text,"+
            " use_people integer,"+
            " is_vip integer,"+
            " is_year_vip integer);";

    private SQLiteDatabase mSQLiteDatabase;
    public HistoryDbManager(Context context) {
        SQLiteDbHelper helper = new SQLiteDbHelper(context.getApplicationContext());
        mSQLiteDatabase = helper.getWritableDatabase();
    }




    public void insertValue(List<HistoryDbBean> list){
        if(list == null)
            return;
        for (HistoryDbBean bean : list)
            insertValue(bean);
    }

    public void insertValue(HistoryDbBean bean){
        ContentValues contentValues = new ContentValues();
        contentValues.put("app_id",bean.getContentCode());
        contentValues.put("user_id",AppConfig.UserID);
        contentValues.put("app_name",bean.getContentName());
        contentValues.put("img_url",bean.getPosterUrl());
        contentValues.put("use_time",bean.getLastUsedTime());
        contentValues.put("use_people",bean.getStartTimes());
        contentValues.put("is_vip",bean.getIsVip());
        contentValues.put("is_year_vip",bean.getIsYearVip());
        mSQLiteDatabase.insert(TABLE_HISTORY,null,contentValues);
    }


    public List<HistoryDbBean> queryHistory(){
        List<HistoryDbBean> list = new ArrayList<>();
        Cursor cursor = mSQLiteDatabase.query(TABLE_HISTORY,null,"user_id=?",new String[]{AppConfig.UserID},null,null,null);
        while (cursor.moveToNext()){
            HistoryDbBean bean = new HistoryDbBean();
            bean.setContentCode(cursor.getString(cursor.getColumnIndex("app_id")));
            bean.setContentName(cursor.getString(cursor.getColumnIndex("app_name")));
            bean.setPosterUrl(cursor.getString(cursor.getColumnIndex("img_url")));
            bean.setStartTimes(cursor.getInt(cursor.getColumnIndex("use_people")));
            bean.setLastUsedTime(cursor.getString(cursor.getColumnIndex("use_time")));
            bean.setIsVip(cursor.getInt(cursor.getColumnIndex("is_vip")));
            bean.setIsYearVip(cursor.getInt(cursor.getColumnIndex("is_year_vip")));
            list.add(bean);
        }
        cursor.close();
        return list;
    }

    public HistoryDbBean queryHistory(String contentCode){
        Cursor cursor = mSQLiteDatabase.query(TABLE_HISTORY,null,"user_id = ? and app_id = ?",new String[]{AppConfig.UserID,contentCode},null,null,null);
        while (cursor.moveToNext()){
            HistoryDbBean bean = new HistoryDbBean();
            bean.setContentCode(cursor.getString(cursor.getColumnIndex("app_id")));
            bean.setContentName(cursor.getString(cursor.getColumnIndex("app_name")));
            bean.setLastUsedTime(cursor.getString(cursor.getColumnIndex("use_time")));
            bean.setPosterUrl(cursor.getString(cursor.getColumnIndex("img_url")));
            bean.setStartTimes(cursor.getInt(cursor.getColumnIndex("use_people")));
            bean.setIsVip(cursor.getInt(cursor.getColumnIndex("is_vip")));
            bean.setIsYearVip(cursor.getInt(cursor.getColumnIndex("is_year_vip")));
            cursor.close();
            return bean;
        }
        cursor.close();
        return null;
    }

    public void delete(String contentCode){
        if(TextUtils.isEmpty(AppConfig.UserID))
            return;
        mSQLiteDatabase.delete(TABLE_HISTORY,"user_id = ? and app_id = ?",new String[]{AppConfig.UserID,contentCode});
    }

    public void updateHistory(String contentCode,String time){
        ContentValues contentValues = new ContentValues();
        contentValues.put("use_time",time);
        mSQLiteDatabase.update(TABLE_HISTORY,contentValues,"user_id = ? and app_id = ?",new String[]{AppConfig.UserID,contentCode});

    }


}
一.创建一个DataBaseHelper DataBaseHelper是一个访问SQLite的助类,提供两个方面的功能 1.getReadableDatebase(),getWriteableDatabase()可以获取SQLiteDatabase对象,通过 2.提供了onCreate()和onUpdate()两个回调函数,允许我们常见和升级数据库是进行使用 A、 在SQLiteOpenHelper的子类当中,必须要有的构造函数 B、该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDataBase对象的时候onCreate 二、创建一个实体person类并且给字段和封装 三、创建一个业务类对SQL的CRUD操作 1.getWritableDatabase()和getReadableDatabase()的区别 ,两个方法都可以获取一个用于操作数据库的SQLiteDatabase实例 2.execSQL(增,删,改都是这个方法)和close();android内部有缓存可关闭也不关闭也行,查询rawQuery是方法 3.在分页有到Cursor(游标)取游标下一个值cursor.moveToNext(),用游标对象接数据 "select * from person limit ?,?" person不能加上where 关键字 4.在删除注意:sb.deleteCharAt(sb.length() - 1); 四、AndroidCRUD业务对SQLite的CRUD操作 1.ContentValues对象的使用 2.android内部insert添加数据的方法,而且values这个不给值也必须要执行,而主键是不是null的其他字段的值是为null 3.insert update query delete 五、单元测试类要注意的 AndroidCRUDService curdService = new AndroidCRUDService(this.getContext()); /* * 注意:getContext必须在我们使用前已经注解进去的,在使用前要实力化,而且是使用后才有上下文 *一般设置为局部对象 */ 六、AndroidManifest.xml的配置 <!-- 配置用户类库android.test.runner测试 --> package jll.sqlitedb; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * *@author Administrator DataBaseHelper是一个访问SQLite的助类,提供两个方面的功能 * 1.getReadableDatebase(),getWriteableDatabase()可以获取SQLiteDatabase对象,通过 * 2.提供了onCreate()和onUpdate()两个回调函数,允许我们常见和升级数据库是进行使用 */ public class DataBaseHelper extends SQLiteOpenHelper { // 给一个默认的SQLite的数据库名 private static final String DataBaseName = "SQLite_DB"; private static final int VERSION = 2; // 在SQLiteOpenHelper的子类当中,必须要有的构造函数 public DataBaseHelper(Context context, String name, CursorFacto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫的于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值