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;
}
}
数据库增删改查
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 开始使用SQLite所需要的几个步骤 1.需要导入的框架:libsqlite3.0.tbd 2.创建Model类L...
- 最近在老师的建议下,参加了一个学习小组,主要了解Spring Cloud微服务架构的应用开发,在初次搭建好环境后,...