XUtils3数据库操作

public class JDApp extends Application{
    
    public static JDApp instance;
    public DbManager db;
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); 
        // 是否输出debug日志, 开启debug会影响性能.
        try {
            initData();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void add(byte[] bt) throws DbException {
        HistoryData hd = new HistoryData(Util.getTime(),bt);
        db.save(hd);
        LogUtil.i("---保存数据");
    }

    private  void initData() throws DbException {
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                //设置数据库名,默认xutils.db
                .setDbName("history.db")
                //设置数据库路径,默认存储在app的私有目录
                //.setDbDir(new File("/mnt/sdcard/"))
                //设置数据库的版本号
                .setDbVersion(3)
                //设置数据库打开的监听
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        //开启数据库支持多线程操作,提升性能,对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setAllowTransaction(true)
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    }
                })
                //设置表创建的监听
                .setTableCreateListener(new DbManager.TableCreateListener() {
                    @Override
                    public void onTableCreated(DbManager db, TableEntity table){
                        Log.i("JAVA", "onTableCreated:" + table.getName());
                    }
                });
        db = x.getDb(daoConfig);
    }
}
public class HistoryDao {
    DbManager db;
    public HistoryDao(){
        this.db=JDApp.instance.db;
    }

    public boolean deleteAll(){
        try {
            db.delete(HistoryData.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List findByUserId(Integer uid) throws DbException {
        Cursor cursor = db.execQuery("select * from history_data where uid="+uid);
        List list = new ArrayList<>();
        while (cursor.moveToNext()){
            HistoryData hd=new HistoryData();
            hd.setUid(cursor.getInt(cursor.getColumnIndex("uid")));
            hd.setMeasureTime(cursor.getString(cursor.getColumnIndex("measureTime")));
            hd.setHistoryByte(cursor.getBlob(cursor.getColumnIndex("historyByte")));
            list.add(hd);
        }
        if (list==null){
            list=new ArrayList<>();
        }
        if (cursor!=null){
            cursor.close();
        }
        return list;
    }

    public List findAll(){
        List all = new ArrayList<>();
        try {
            all = db.findAll(HistoryData.class);
            Log.d("plg",all.size()+"");
        } catch (DbException e) {
            e.printStackTrace();
            return all;
        }
        return all;
    }
}
@Table(name ="historyData",onCreated = "")
public class HistoryData {
    @Column(name="id",isId=true,autoGen=true)
    private int id;

    @Column(name="measureTime")
    private String measureTime;
    @Column(name="historyByte")
    private byte[] historyByte;

    public byte[] getHistoryByte() {
        return historyByte;
    }

    public void setHistoryByte(byte[] historyByte) {
        this.historyByte = historyByte;
    }

    public String getMeasureTime() {
        return measureTime;
    }

    public void setMeasureTime(String measureTime) {
        this.measureTime = measureTime;
    }

    public HistoryData(){

    }

    public HistoryData(String measureTime, byte[] historyByte) {
        this.measureTime = measureTime;
        this.historyByte = historyByte;
    }
}

你可能感兴趣的:(XUtils3数据库操作)