public class FinancingDbHelper extends SQLiteOpenHelper {
// 表名
public static final String TABLE_NAME =
"financing";
// 用于标识是哪个申请的融资信息的字段
public static final String CLIENTID =
"clientId";
// 方案类型
public static final String PRE_TYPE_ID =
"type_id";
public static final String PRE_TYPE =
"type_name";
// 方案名称
public static final String PRE_PLAN_ID =
"plan_id";
public static final String PRE_PLAN =
"plan_name";
// 指导价
public static final String PRE_GUIDE_PRICE =
"guideprice";
// GPS硬件
public static final String PRE_GPS =
"GPS";
// 制造商
public static final String PRE_MAN_ID =
"man_id";
public static final String PRE_MAN =
"man_name";
// 品牌
public static final String PRE_BRAND_ID =
"brand_id";
public static final String PRE_BRAND =
"brand_name";
// 车型
public static final String PRE_CAR_ID =
"car_id";
public static final String PRE_CAR =
"car_name";
// 车辆售价
public static final String PRE_PRICE =
"price";
public final String PRE_SEAT =
"seat";
public final String PRE_TONNAGE =
"tonnage";
public final String PRE_DISPLACEMENT =
"Displacement";
// 融资期限
public static final String PRE_FINANCING_LIMIT =
"financing_limit";
// 抵押城市
public final String PRE_CITY =
"city";
// 营运
public final String PRE_OPERATE =
"operate";
// 融延保
public final String PRE_RONG_YAN_BAO =
"rong_yan_bao";
// 融保险
public final String PRE_RONG_BAO_XIAN =
"rong_bao_xian";
// 融安心宝
public final String PRE_RONG_ANXIN_BAO =
"rong_anxin_bao";
// 安心宝
public static final String PRE_ANXIN_BAO =
"anxin_bao";
// 三方责任限额
public final String PRE_RESPONSE_LIMIT =
"responsibility_limit";
// 车上人员责任险(司机)
public final String PRE_ZEREN_DRIVER =
"zeren_insurance_driver";
// 车上人员责任险 (乘客)
public final String PRE_ZEREN_PASSENGER =
"zeren_insurance_passenger";
// 车上人员责任险不计免赔
public final String PRE_ZEREN_DEDUCTIBLE =
"zeren_insurance_deductible";
// 车辆划伤险
public final String PRE_SCRATCH_INSURANCE =
"scratch_insurance";
// 车辆划伤不计赔
public final String PRE_SCRATCH_DEDUCTIBLE =
"scratch_insurance_deductible";
// 玻璃险
public final String PRE_GLASS_INSURANCE =
"glass_insurance";
// 专修险
public final String PRE_REPAIR_INSURANCE =
"repair_insurance";
// 购置税
public final String PRE_PURCHASE_TAX =
"purchase_tax";
// 延保
public static final String PRE_EX_INSURANCE =
"ex_insurance";
// 车船税
public final String PRE_TRAVEL_TAX =
"travel_tax";
// 商业保险
public final String PRE_BUS_INSURANCE =
"bus_insurance";
// 交强险
public final String PRE_COST_INSURANCE =
"insurance_cost";
// 首付比例
public final String PRE_SHOUFU =
"shoufu";
// 首付金额
public final String PRE_SHOWFU_MONEY =
"shoufu_money";
// 首付比例逻辑
public final String PRE_SHOWFU_LOGIC =
"shoufu_logic";
// 尾付比例
public final String PRE_WEIFU =
"weifu";
// 尾付金额
public final String PRE_WEIFU_MONEY =
"weifu_money";
// 尾付比例逻辑
public final String PRE_WEIFU_LOGIC =
"weifu_logic";
// 融资金额
public final String PRE_FINANCING_MONEY =
"financing_money";
// 手续是否分期
public final String PRE_STAGING =
"staging";
// 手续费率
public final String PRE_RATE =
"rate";
// 手续费
public final String PRE_PROCEDURE =
"procedure_money";
// 保证金率
public final String PRE_MARGIN =
"margin";
// 保证金
public final String PRE_MARGIN_MONEY =
"margin_money";
// 保证金比例逻辑
public final String PRE_MARGIN_LOGIC =
"margin_logic";
// 投资总额
public final String PRE_TOTAL =
"total";
// 销售经理
public final String PRE_SALES_MANAGER_ID =
"sales_manager_id";
public static final String PRE_SALES_MANAGER =
"sales_manager";
// 销售助理
public final String PRE_SALES_ASSISTANT_ID =
"sales_assistant_id";
public static final String PRE_SALES_ASSISTANT =
"sales_assistant";
// 开户银行
public static final String PRE_BANK =
"bank";
// 开户姓名
public static final String PRE_NAME =
"name";
// 银行账号
public static final String PRE_BANK_NUMBER =
"bank_number";
// 征信是否后置
public static final String PRE_CREDIT =
"credit";
// 备注
public static final String PRE_REMARKS =
"remarks";
public static final String PRE_INSURANCE =
"insurance";
public static final String PRE_LOADPLAN =
"loadplan";
// 页标识
public static final String PRE_FRIST =
"frist";
public static final String PRE_SECOND =
"second";
public static final String PRE_THIRD =
"third";
public static final String PRE_FOUTH =
"fouth";
public static final String PRE_FIFTH =
"fifth";
private static FinancingDbHelper financingDbHelper =
null;
private FinancingDbHelper(Context context) {
super(context, "FinancingDb.db",
null,
1);
}
public static FinancingDbHelper getIns(Context context) {
if (financingDbHelper ==
null) {
synchronized (FinancingDbHelper.class) {
if (financingDbHelper ==
null) {
financingDbHelper =
new FinancingDbHelper(context);
}
}
}
return financingDbHelper;
}
@Override
public void onCreate(SQLiteDatabase db) {
// 建表
String sql_CreateTable =
" create table if not exists " +
TABLE_NAME
+
"(_id integer primary key autoincrement,"
+ CLIENTID +
" text," // 用于标识是哪个申请的融资信息的字段
// 方案类型
+ PRE_TYPE_ID +
" text,"
+ PRE_TYPE +
" text,"
// 方案名称
+ PRE_PLAN_ID +
" text,"
+ PRE_PLAN +
" text,"
+ PRE_GUIDE_PRICE +
" text,"// 指导价
+ PRE_GPS +
" text," // GPS硬件
// 制造商
+ PRE_MAN_ID +
" text,"
+ PRE_MAN +
" text,"
// 品牌
+ PRE_BRAND_ID +
" text,"
+ PRE_BRAND +
" text,"
// 车型
+ PRE_CAR_ID +
" text,"
+ PRE_CAR +
" text,"
// 车辆售价
+ PRE_PRICE +
" text,"
+ PRE_SEAT +
" text,"
+ PRE_TONNAGE +
" text,"
+ PRE_DISPLACEMENT +
" text,"
+ PRE_FINANCING_LIMIT +
" text," // 融资期限
+ PRE_CITY +
" text," // 抵押城市
+ PRE_OPERATE +
" text,"//只能写true或false 营运
+ PRE_RONG_YAN_BAO +
" text,"//只能写true或false 融延保
+ PRE_RONG_BAO_XIAN +
" text,"//只能写true或false 融保险
+ PRE_RONG_ANXIN_BAO +
" text,"//只能写true或false 融安心宝
+ PRE_ANXIN_BAO +
" text," // 安心宝
+ PRE_RESPONSE_LIMIT +
" text," // 三方责任限额
+ PRE_ZEREN_DRIVER +
" text," // 车上人员责任险(司机)
+ PRE_ZEREN_PASSENGER +
" text," // 车上人员责任险 (乘客)
+ PRE_ZEREN_DEDUCTIBLE +
" text," // 车上人员责任险不计免赔
+ PRE_SCRATCH_INSURANCE +
" text," // 车辆划伤险
+ PRE_SCRATCH_DEDUCTIBLE +
" text," // 车辆划伤不计赔
+ PRE_GLASS_INSURANCE +
" text," // 玻璃险
+ PRE_REPAIR_INSURANCE +
" text," // 专修险
+ PRE_PURCHASE_TAX +
" text," // 购置税
+ PRE_EX_INSURANCE +
" text," // 延保
+ PRE_TRAVEL_TAX +
" text," // 车船税
+ PRE_BUS_INSURANCE +
" text," // 商业保险
+ PRE_COST_INSURANCE +
" text," // 交强险
+ PRE_SHOUFU +
" text," // 首付比例
+ PRE_SHOWFU_MONEY +
" text," // 首付金额
+ PRE_SHOWFU_LOGIC +
" text," // 首付比例逻辑
+ PRE_WEIFU +
" text," // 尾付比例
+ PRE_WEIFU_MONEY +
" text," // 尾付金额
+ PRE_WEIFU_LOGIC +
" text," // 尾付比例逻辑
+ PRE_FINANCING_MONEY +
" text," // 融资金额
+ PRE_STAGING +
" text,"//只能写true或false 手续是否分期
+ PRE_RATE +
" text," // 手续费率
+ PRE_PROCEDURE +
" text," // 手续费
+ PRE_MARGIN +
" text," // 保证金率
+ PRE_MARGIN_MONEY +
" text," // 保证金
+ PRE_MARGIN_LOGIC +
" text," // 保证金比例逻辑
+ PRE_TOTAL +
" text," // 投资总额
// 销售经理
+ PRE_SALES_MANAGER_ID +
" text,"
+ PRE_SALES_MANAGER +
" text,"
// 销售助理
+ PRE_SALES_ASSISTANT_ID +
" text,"
+ PRE_SALES_ASSISTANT +
" text,"
+ PRE_BANK +
" text," // 开户银行
+ PRE_NAME +
" text," // 开户姓名
+ PRE_BANK_NUMBER +
" text," // 银行账号
+ PRE_CREDIT +
" text,"//只能写true或false 征信是否后置
+ PRE_REMARKS +
" text," // 备注
+ PRE_INSURANCE +
" text,"
+ PRE_LOADPLAN +
" text,"
// 页标识
+ PRE_FRIST +
" text,"//只能写true或false
+ PRE_SECOND +
" text,"//只能写true或false
+ PRE_THIRD +
" text,"//只能写true或false
+ PRE_FOUTH +
" text,"//只能写true或false
+ PRE_FIFTH +
" text"//只能写true或false
+
")";
//执行sql语句,创建数据库表
db.execSQL(sql_CreateTable);
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion,
int newVersion) {
// 版本发生变化时更新数据库
db.delete(TABLE_NAME,
null,
null);
onCreate(db);
}
}
然后在定义一个文件对表进行操作:
package com.mesada.financing.dbutils;
import com.baidu.a.a.a.c;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class FinancingDbDao {
FinancingDbHelper financingDbHelper;
public FinancingDbDao(Context context) {
// 生成FinancingDbHelper对象
this.financingDbHelper = FinancingDbHelper.getIns(context);
}
/**
* 打开融资信息模块时首先向表中插入一条数据
*
* @param clientId
* 用于标识是哪个申请的融资信息的字段
*/
public void insert(String clientId) {
SQLiteDatabase db = financingDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FinancingDbHelper.CLIENTID, clientId);
values.put(FinancingDbHelper.PRE_FRIST, "false");
values.put(FinancingDbHelper.PRE_SECOND, "false");
values.put(FinancingDbHelper.PRE_THIRD, "false");
values.put(FinancingDbHelper.PRE_FOUTH, "false");
values.put(FinancingDbHelper.PRE_FIFTH, "false");
values.put(FinancingDbHelper.PRE_TYPE_ID, "0");
values.put(FinancingDbHelper.PRE_TYPE, "");
values.put(FinancingDbHelper.PRE_PLAN_ID, "0");
values.put(FinancingDbHelper.PRE_PLAN, "");
values.put(FinancingDbHelper.PRE_MAN_ID, "0");
values.put(FinancingDbHelper.PRE_MAN, "");
values.put(FinancingDbHelper.PRE_BRAND_ID, "0");
values.put(FinancingDbHelper.PRE_BRAND, "");
values.put(FinancingDbHelper.PRE_CAR_ID, "0");
values.put(FinancingDbHelper.PRE_CAR, "");
values.put(FinancingDbHelper.PRE_GUIDE_PRICE, "0");
values.put(financingDbHelper.PRE_SEAT, "0");
values.put(financingDbHelper.PRE_TONNAGE, "0");
values.put(financingDbHelper.PRE_DISPLACEMENT, "0");
values.put(FinancingDbHelper.PRE_PRICE, "0");
values.put(FinancingDbHelper.PRE_GPS, "0");
values.put(FinancingDbHelper.PRE_FINANCING_LIMIT, "");
values.put(financingDbHelper.PRE_CITY, "");
values.put(financingDbHelper.PRE_OPERATE, "false");
values.put(financingDbHelper.PRE_RONG_YAN_BAO, "false");
values.put(financingDbHelper.PRE_RONG_BAO_XIAN, "true");
values.put(financingDbHelper.PRE_RONG_ANXIN_BAO, "true");
values.put(FinancingDbHelper.PRE_ANXIN_BAO, "0");
values.put(financingDbHelper.PRE_RESPONSE_LIMIT, "20");
values.put(financingDbHelper.PRE_ZEREN_DRIVER, "0");
values.put(financingDbHelper.PRE_ZEREN_PASSENGER, "0");
values.put(financingDbHelper.PRE_ZEREN_DEDUCTIBLE, "false");
values.put(financingDbHelper.PRE_SCRATCH_INSURANCE, "0");
values.put(financingDbHelper.PRE_SCRATCH_DEDUCTIBLE, "false");
values.put(financingDbHelper.PRE_GLASS_INSURANCE, "无");
values.put(financingDbHelper.PRE_REPAIR_INSURANCE, "false");
values.put(financingDbHelper.PRE_PURCHASE_TAX, "0");
values.put(FinancingDbHelper.PRE_EX_INSURANCE, "0");
values.put(financingDbHelper.PRE_TRAVEL_TAX, "");
values.put(financingDbHelper.PRE_BUS_INSURANCE, "");
values.put(financingDbHelper.PRE_COST_INSURANCE, "");
values.put(financingDbHelper.PRE_SHOUFU, "0");
values.put(financingDbHelper.PRE_SHOWFU_MONEY, "0");
values.put(financingDbHelper.PRE_SHOWFU_LOGIC, "0");
values.put(financingDbHelper.PRE_WEIFU, "0");
values.put(financingDbHelper.PRE_WEIFU_MONEY, "0");
values.put(financingDbHelper.PRE_WEIFU_LOGIC, "0");
values.put(financingDbHelper.PRE_FINANCING_MONEY, "0");
values.put(financingDbHelper.PRE_STAGING, "false");
values.put(financingDbHelper.PRE_RATE, "0");
values.put(financingDbHelper.PRE_PROCEDURE, "0");
values.put(financingDbHelper.PRE_MARGIN, "");
values.put(financingDbHelper.PRE_MARGIN_MONEY, "0");
values.put(financingDbHelper.PRE_MARGIN_LOGIC, "0");
values.put(financingDbHelper.PRE_SALES_MANAGER_ID, "0");
values.put(FinancingDbHelper.PRE_SALES_MANAGER, "");
values.put(financingDbHelper.PRE_SALES_ASSISTANT_ID, "0");
values.put(FinancingDbHelper.PRE_SALES_ASSISTANT, "");
values.put(FinancingDbHelper.PRE_BANK, "");
values.put(FinancingDbHelper.PRE_BANK_NUMBER, "");
values.put(FinancingDbHelper.PRE_CREDIT, "否");
values.put(FinancingDbHelper.PRE_REMARKS, "");
db.insert(FinancingDbHelper.TABLE_NAME, null, values);
db.close();
}
/**
* @param clientId 用于标识是哪个申请的融资信息的字段
* @param column 要查询的字段
* @return 返回要查询的字段的值
*/
public String query(String clientId, String column) {
SQLiteDatabase db = financingDbHelper.getReadableDatabase();
Cursor cursor = db.query(FinancingDbHelper.TABLE_NAME, new String[] { column },
FinancingDbHelper.CLIENTID + "=?", new String[] { clientId },
null, null, null);
String info=null;
while (cursor.moveToNext()) {
info=cursor.getString(0);
}
cursor.close();
db.close();
return info;
}
/**
* 该方法用于更新数据库表中指定字段的值
* @param clientId 用于标识是哪个申请的融资信息的字段
* @param column 要保存的字段
* @param info 要保存的字段的值
*/
public void update(String clientId, String column,String info){
SQLiteDatabase db=financingDbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(column, info);
db.update(FinancingDbHelper.TABLE_NAME, values, FinancingDbHelper.CLIENTID+"=?", new String[]{clientId});
db.close();
}
/**
* @param clientId 要重置的数据的标识
*/
public void delete(String clientId) {
SQLiteDatabase db=financingDbHelper.getWritableDatabase();
db.delete(FinancingDbHelper.TABLE_NAME, FinancingDbHelper.CLIENTID+"=?", new String[]{clientId});
db.close();
}
}
但是后来完成后发现由于此功能对数据库表的操作比较频繁,这样的结果就是较耗时,不符合要求。
经过研究与查资料,发现xutils中的dbutils可以使序列化后的对象变成数据库表,取出来时也是对象,大家都知道对象的好处,因此我就决定使用dbutils。
首先,创建一个基础类,此类实现了serializable借口,如下:
package com.mesada.data.model;
import java.io.Serializable;
import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Id;
public class FinancingMsgModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3866706611954627888L;
@Id
public int id;
// 用于标识是哪个申请的融资信息的字段
@Column
public String clientId;
// 方案类型
@Column
public String type_id;
@Column
public String type;
// 方案名称
@Column
public String plan_id;
@Column
public String plan;
// 指导价
@Column
public String guideprice;
// GPS硬件
@Column
public String GPS;
// 制造商
@Column
public String man_id;
@Column
public String man;
// 品牌
@Column
public String brand_id;
@Column
public String brand;
// 车型
@Column
public String car_id;
@Column
public String car;
// 车辆售价
@Column
public String price;
@Column
public String seat;
@Column
public String tonnage;
@Column
public String displacement;
// 融资期限
@Column
public String financing_limit;
// 抵押城市
@Column
public String city;
// 营运
@Column
public String operate;
// 融延保
@Column
public String rong_yan_bao;
// 融保险
@Column
public String rong_bao_xian;
// 融安心宝
@Column
public String rong_anxin_bao;
// 安心宝
@Column
public String anxin_bao;
// 三方责任限额
@Column
public String responsibility_limit;
// <