数据持久化
有些信息需要保存在本地
三种方法
1.保存在文件中,需要的时候进行读写 io 比较复杂麻烦 不推荐使用
2.保存在SharedPreFerence中
3.保存在数据库中SQLite
第一种方法是java中的方法,利用流将要保存的数据写入到某个目录下的文件中,在需要的时候进行读写。
Android 中提供了一种openFileoutput()的方法 用这种方法可以将流写入指定文件中
第二种方法 是保存在SharedPreFrence
SharedPrefrenc是android 提供的一种 存储方式 使用较为简单
这里提供一个工具类可以更好的使用
public static class UserInfoSpUtils{
public final static String SP_NAME = "userinfo";
private static SharedPreferences sp;
public static void saveBoolean(Context context, String key, boolean value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putBoolean(key, value).commit();
}
public static void saveInt(Context context, String key, int value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putInt(key, value).commit();
}
public static void saveLong(Context context, String key, Long value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putLong(key, value).commit();
}
public static void saveString(Context context, String key, String value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putString(key, value).commit();
}
public static int getInt(Context context, String key, int defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getInt(key, defValue);
}
public static String getString(Context context, String key, String defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getString(key, defValue);
}
public static boolean getBoolean(Context context, String key,
boolean defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getBoolean(key, defValue);
}
public static Long getLong(Context context, String key, Long defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getLong(key, defValue);
}
}
第三种 数据库 SQLite
android 中提供了一个SQLiteOpenHelper 类用来创建数据库
代码
public class StatOpenHelper extends SQLiteOpenHelper {
//部门数据库表格
private final String departmentSQL = "CREATE TABLE IF NOT EXISTS DepartmentFee(" +
"amount TEXT," +//金额
"accountName TEXT," +//报销科目
"expenseId TEXT," +//单据id
"accountId TEXT," +//科目id
"month TEXT," +//月
"year TEXT," +//年
"departmentId TEXT," +// ID
"departmentCode TEXT," +//code
"departmentName TEXT," +//部门名称
"date TEXT," +//时间
"type TEXT" +//统计类型
")";
//事业部数据库表格
private final String businessUnitSQL = "CREATE TABLE IF NOT EXISTS BusinessUnitFee(" +
"amount TEXT," +
"businessUnitCode TEXT," +
"expenseId TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"businessUnitName TEXT," +
"businessUnitId TEXT," +
"month TEXT," +
"year TEXT," +
"date TEXT," +
"type TEXT" +
")";
//客户数据库表格
private final String clientSQL = "CREATE TABLE IF NOT EXISTS ClientFee(" +
"amount TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"clientCode TEXT," +
"clientName TEXT," +
"month TEXT," +
"year TEXT," +
"date TEXT," +
"type TEXT," +
"clientId TEXT" +
")";
//项目数据库表格
private final String projectSQL = "CREATE TABLE IF NOT EXISTS ProjectFee(" +
"amount TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"projectCode TEXT," +
"month TEXT," +
"year TEXT," +
"projectId TEXT," +
"date TEXT," +
"type TEXT," +
"projectName TEXT" +
")";
public StatOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public StatOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql=
"CREATE TABLE IF NOT EXISTS DepartmentFee(amount TEXT,accountName TEXT,expenseId TEXT,accountId TEXT,month TEXT,year TEXT,departmentId TEXT,departmentCode TEXT,departmentName TEXT,date TEXT,type TEXT)"
db.execSQL(sql);
}
//该方法用于数据库升级时使用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
基本增删改查
增 insert方法
ContentValues cv = new ContentValues();//实例化一个ContentValues用来装载待插入的数据cv.put("username","Jack Johnson");//添加用户名
cv.put("password","iLovePopMusic"); //添加密码
db.insert("user",null,cv);//执行插入操作
删 delete
String whereClause = "username=?";//删除的条件
String[] whereArgs = {"Jack Johnson"};//删除的条件参数
db.delete("user",whereClause,whereArgs);//执行删除
改
ContentValues cv = new ContentValues();//实例化ContentValues
cv.put("password","iHatePopMusic");//添加要更改的字段及内容
String whereClause = "username=?";//修改条件
String[] whereArgs = {"Jack Johnson"};//修改条件的参数
db.update("user",cv,whereClause,whereArgs);//执行修改
查
数据查询相对前面几种方法就复杂一些了,因为查询会带有很多条件
通过query实现查询的
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
各参数说明:
table:表名称
colums:列名称数组
selection:条件子句,相当于where
selectionArgs:条件语句的参数数组
groupBy:分组
having:分组条件
orderBy:排序类
limit:分页查询的限制
Cursor:返回值,相当于结果集ResultSet
使用数据库的时候最好开启事务
db.beginTransaction()
开启事务
db.setTransactionSuccessful()
成功
db.endTransaction
结束事务
事务一定要结束 最好使用try{}catch{}finally{}的方式 让结束事务在finally中
升级数据库
sqldrop = "DROP TABLE SearchRecord";
db.execSQL(sqldrop);
删掉之前的表
数据库升级最佳写法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL("alter table Book add column category_id integer");
default:
}
}
注意 上述数据库升级方法中的switch方法中没有写break 并不是忘记写,而是要保证内部的方法都执行到 预防跨版本升级的问题
有些信息需要保存在本地
三种方法
1.保存在文件中,需要的时候进行读写 io 比较复杂麻烦 不推荐使用
2.保存在SharedPreFerence中
3.保存在数据库中SQLite
第一种方法是java中的方法,利用流将要保存的数据写入到某个目录下的文件中,在需要的时候进行读写。
Android 中提供了一种openFileoutput()的方法 用这种方法可以将流写入指定文件中
第二种方法 是保存在SharedPreFrence
SharedPrefrenc是android 提供的一种 存储方式 使用较为简单
这里提供一个工具类可以更好的使用
public static class UserInfoSpUtils{
public final static String SP_NAME = "userinfo";
private static SharedPreferences sp;
public static void saveBoolean(Context context, String key, boolean value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putBoolean(key, value).commit();
}
public static void saveInt(Context context, String key, int value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putInt(key, value).commit();
}
public static void saveLong(Context context, String key, Long value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putLong(key, value).commit();
}
public static void saveString(Context context, String key, String value) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
sp.edit().putString(key, value).commit();
}
public static int getInt(Context context, String key, int defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getInt(key, defValue);
}
public static String getString(Context context, String key, String defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getString(key, defValue);
}
public static boolean getBoolean(Context context, String key,
boolean defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getBoolean(key, defValue);
}
public static Long getLong(Context context, String key, Long defValue) {
if (sp == null)
sp = context.getSharedPreferences(SP_NAME, 0);
return sp.getLong(key, defValue);
}
}
第三种 数据库 SQLite
android 中提供了一个SQLiteOpenHelper 类用来创建数据库
代码
public class StatOpenHelper extends SQLiteOpenHelper {
//部门数据库表格
private final String departmentSQL = "CREATE TABLE IF NOT EXISTS DepartmentFee(" +
"amount TEXT," +//金额
"accountName TEXT," +//报销科目
"expenseId TEXT," +//单据id
"accountId TEXT," +//科目id
"month TEXT," +//月
"year TEXT," +//年
"departmentId TEXT," +// ID
"departmentCode TEXT," +//code
"departmentName TEXT," +//部门名称
"date TEXT," +//时间
"type TEXT" +//统计类型
")";
//事业部数据库表格
private final String businessUnitSQL = "CREATE TABLE IF NOT EXISTS BusinessUnitFee(" +
"amount TEXT," +
"businessUnitCode TEXT," +
"expenseId TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"businessUnitName TEXT," +
"businessUnitId TEXT," +
"month TEXT," +
"year TEXT," +
"date TEXT," +
"type TEXT" +
")";
//客户数据库表格
private final String clientSQL = "CREATE TABLE IF NOT EXISTS ClientFee(" +
"amount TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"clientCode TEXT," +
"clientName TEXT," +
"month TEXT," +
"year TEXT," +
"date TEXT," +
"type TEXT," +
"clientId TEXT" +
")";
//项目数据库表格
private final String projectSQL = "CREATE TABLE IF NOT EXISTS ProjectFee(" +
"amount TEXT," +
"accountName TEXT," +
"accountId TEXT," +
"projectCode TEXT," +
"month TEXT," +
"year TEXT," +
"projectId TEXT," +
"date TEXT," +
"type TEXT," +
"projectName TEXT" +
")";
public StatOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public StatOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql=
"CREATE TABLE IF NOT EXISTS DepartmentFee(amount TEXT,accountName TEXT,expenseId TEXT,accountId TEXT,month TEXT,year TEXT,departmentId TEXT,departmentCode TEXT,departmentName TEXT,date TEXT,type TEXT)"
db.execSQL(sql);
}
//该方法用于数据库升级时使用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
基本增删改查
增 insert方法
ContentValues cv = new ContentValues();//实例化一个ContentValues用来装载待插入的数据cv.put("username","Jack Johnson");//添加用户名
cv.put("password","iLovePopMusic"); //添加密码
db.insert("user",null,cv);//执行插入操作
删 delete
String whereClause = "username=?";//删除的条件
String[] whereArgs = {"Jack Johnson"};//删除的条件参数
db.delete("user",whereClause,whereArgs);//执行删除
改
ContentValues cv = new ContentValues();//实例化ContentValues
cv.put("password","iHatePopMusic");//添加要更改的字段及内容
String whereClause = "username=?";//修改条件
String[] whereArgs = {"Jack Johnson"};//修改条件的参数
db.update("user",cv,whereClause,whereArgs);//执行修改
查
数据查询相对前面几种方法就复杂一些了,因为查询会带有很多条件
通过query实现查询的
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
各参数说明:
table:表名称
colums:列名称数组
selection:条件子句,相当于where
selectionArgs:条件语句的参数数组
groupBy:分组
having:分组条件
orderBy:排序类
limit:分页查询的限制
Cursor:返回值,相当于结果集ResultSet
使用数据库的时候最好开启事务
db.beginTransaction()
开启事务
db.setTransactionSuccessful()
成功
db.endTransaction
结束事务
事务一定要结束 最好使用try{}catch{}finally{}的方式 让结束事务在finally中
升级数据库
sqldrop = "DROP TABLE SearchRecord";
db.execSQL(sqldrop);
删掉之前的表
数据库升级最佳写法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL("alter table Book add column category_id integer");
default:
}
}
注意 上述数据库升级方法中的switch方法中没有写break 并不是忘记写,而是要保证内部的方法都执行到 预防跨版本升级的问题