数据库增删改查

public class SetDao {
    private SQLiteDatabase db;

    public SQLiteDatabase getDb() {
        return db;
    }

    public void setDb(SQLiteDatabase db) {
        this.db = db;
        //初始化
        init();
    }

    //初始化
    public void init() {
        boolean b = isUsable();
        if(b) {
            createTable();
        }
    }

    /**
     * 判断当前连接是否可用
     * @return
     */
    public boolean isUsable() {
        boolean b = false;
        try {
            if(null != db) {
                if(db.isOpen()) {
                    b = true;
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return b;
    }

    /**
     * 创建configuration表
     * @return
     */
    public boolean createTable() {
        boolean b = false;
        try {
            db.execSQL("CREATE TABLE if not exists configuration (dimage_uri VARCHAR,dpay_uri VARCHAR,dcloud_uri VARCHAR,din_uri VARCHAR,dversioncode_uri VARCHAR)");
            b = true;
        } catch (SQLException e) {
            throw e;
        }

        return b;
    }

    /**
     * 插入configuration表
     * @param configuration
     * @return
     */
    public boolean insert(Configuration configuration) {
        boolean b = true;
        try {

            ContentValues values = new ContentValues();
            values.put("dimage_uri", configuration.getDimage_uri());
            values.put("dpay_uri", configuration.getDpay_uri());
            values.put("dcloud_uri", configuration.getDcloud_uri());
            values.put("din_uri", configuration.getDin_uri());
            values.put("dversioncode_uri", configuration.getDversioncode_uri());
            db.insert("configuration", null, values);
            System.gc();
            b = true;
        } catch (Exception e) {
            throw e;
        }
        return b;
    }

    /**
     * 更新configuration表
     * @param configuration
     * @return
     */
    public boolean update(Configuration configuration) {
        boolean b = false;
        try {
            db.execSQL("update configuration set dimage_uri=?,dpay_uri=?,dcloud_uri=?,din_uri=?,dversioncode_uri=?",
                    new Object[]{configuration.getDimage_uri(),configuration.getDpay_uri(),configuration.getDcloud_uri(),configuration.getDin_uri(),configuration.getDversioncode_uri()});
            db.close();
        } catch (SQLException e) {
            throw e;
        }

        return b;
    }

    /**
     * 删除configuration
     * @param dimage_uri
     * @return
     */
    public boolean delete(String dimage_uri){
        boolean b = false;
        try {
            db.execSQL("delete configuration where dimage_uri = ?", new String[]{dimage_uri});
            b = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return b;

    }

    /**
     * 删除表中所有数据
     * @return
     */
    public boolean deleteAll() {
        boolean b = false;
        try {
            db.execSQL("delete from configuration");
            b = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 查询表
     * @return
     */
    public Configuration findConfiguration() {
        Configuration configuration = new Configuration();
        try {
            Cursor c = db.rawQuery("select * from configuration", null);
            while (c.moveToNext()) {

                String dimage_uri = c.getString(c.getColumnIndex("dimage_uri"));
                String dpay_uri = c.getString(c.getColumnIndex("dpay_uri"));
                String dcloud_uri = c.getString(c.getColumnIndex("dcloud_uri"));
                String din_uri = c.getString(c.getColumnIndex("din_uri"));
                String dversioncode_uri = c.getString(c.getColumnIndex("dversioncode_uri"));
                configuration.setDimage_uri(dimage_uri);
                configuration.setDpay_uri(dpay_uri);
                configuration.setDcloud_uri(dcloud_uri);
                configuration.setDin_uri(din_uri);
                configuration.setDversioncode_uri(dversioncode_uri);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return configuration;
    }
}

你可能感兴趣的:(数据库增删改查)