package cn.com.basic.face.service.sqlite;
|
|
import android.content.ContentValues;
|
import android.database.Cursor;
|
import android.database.sqlite.SQLiteDatabase;
|
|
import cn.com.basic.face.base.Config;
|
|
/**
|
* 数据同步dao层
|
*
|
*/
|
public class DataSynchronDao {
|
|
public static DataSynchronDao instance = new DataSynchronDao();
|
|
//##################增删改查案例##########################
|
|
|
/***
|
* 创建一个表
|
* @param db
|
*/
|
private void createTable(SQLiteDatabase db){
|
//创建表SQL语句
|
String stu_table="create table usertable(_id integer primary key autoincrement,sname text,snumber text)";
|
//执行SQL语句
|
db.execSQL(stu_table);
|
}
|
|
|
|
|
|
/***
|
* 添加案例
|
* @param db
|
*
|
* 参数1 表名称,
|
参数2 空列的默认值
|
参数3 ContentValues类型的一个封装了列名称和列值的Map;
|
()
|
*/
|
private void insert(SQLiteDatabase db){
|
//实例化常量值
|
ContentValues cValue = new ContentValues();
|
//添加用户名
|
cValue.put("sname","xiaoming");
|
//添加密码
|
cValue.put("snumber","01005");
|
//调用insert()方法插入数据
|
db.insert("stu_table",null,cValue);
|
}
|
|
|
/***
|
* 删除数据
|
* @param db
|
* 参数1 表名称
|
参数2 删除条件
|
参数3 删除条件值数组
|
*/
|
private void delete(SQLiteDatabase db) {
|
//删除条件
|
String whereClause = "id=?";
|
//删除条件参数
|
String[] whereArgs = {String.valueOf(2)};
|
//执行删除
|
db.delete("stu_table",whereClause,whereArgs);
|
}
|
|
|
/***
|
* 修改数据
|
* @param db
|
*
|
* 参数1 表名称
|
参数2 跟行列ContentValues类型的键值对Key-Value
|
参数3 更新条件(where字句)
|
参数4 更新条件数组
|
*/
|
private void update(SQLiteDatabase db) {
|
//实例化内容值
|
ContentValues values = new ContentValues();
|
//在values中添加内容
|
values.put("snumber","101003");
|
//修改条件
|
String whereClause = "id=?";
|
//修改添加参数
|
String[] whereArgs={"2"};
|
//修改
|
db.update("usertable",values,whereClause,whereArgs);
|
}
|
|
|
/***
|
* 查询数据
|
* @param db
|
*
|
在Android中查询数据是通过Cursor类来实现的,当我们使用SQLiteDatabase.query()方法时,会得到一个Cursor对象,Cursor指向的就是每一条数据。
|
它提供了很多有关查询的方法,具体方法如下:
|
public Cursor query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit);
|
各个参数的意义说明:
|
参数table:表名称
|
参数columns:列名称数组
|
参数selection:条件字句,相当于where
|
参数selectionArgs:条件字句,参数数组
|
参数groupBy:分组列
|
参数having:分组条件
|
参数orderBy:排序列
|
参数limit:分页查询限制
|
参数Cursor:返回值,相当于结果集ResultSet
|
Cursor是一个游标接口,提供了遍历查询结果的方法,如移动指针方法move(),获得列值方法getString()等.
|
|
|
方法描述
|
getCount() :获得总的数据项数
|
isFirst() :判断是否第一条记录
|
isLast() :判断是否最后一条记录
|
moveToFirst() :移动到第一条记录
|
moveToLast() :移动到最后一条记录
|
move(int offset) :移动到指定记录
|
moveToNext() :移动到下一条记录
|
moveToPrevious() :移动到上一条记录
|
getColumnIndexOrThrow(String columnName) :根据列名称获得列索引
|
getInt(int columnIndex) :获得指定列索引的int类型值
|
getString(int columnIndex) :获得指定列缩影的String类型值
|
*/
|
private void query(SQLiteDatabase db) {
|
//查询获得游标
|
Cursor cursor = db.query("usertable",null,null,null,null,null,null);
|
|
//判断游标是否为空
|
if(cursor.moveToFirst()){
|
//遍历游标
|
for(int i=0;i<cursor.getCount();i++){
|
cursor.move(i);
|
//获得ID
|
int id = cursor.getInt(0);
|
//获得用户名
|
String username=cursor.getString(1);
|
//获得密码
|
String password=cursor.getString(2);
|
//输出用户信息
|
System.out.println(id+":"+username+":"+password);
|
}
|
}
|
}
|
|
|
|
|
|
//#######################以上为增删改查DEMO#################################
|
|
|
|
|
/**
|
* 获取dao对象
|
* @return
|
*/
|
public static DataSynchronDao getInstance() {
|
return instance;
|
}
|
|
/**
|
* 单向同步,从客户端同步到服务器端
|
* @param clientTable
|
* @param serviceTable
|
*/
|
public static void clientToService(String clientTable,String serviceTable){
|
|
SQLiteDatabase db = Config.sqlMap.getDb();
|
db.beginTransaction();
|
db.execSQL("");
|
db.endTransaction();
|
//1.根据上次同步时间找出需要上传的数据
|
|
//2.发送数据给服务器端
|
}
|
/**
|
* 单向同步,从服务端同步到客户端
|
* @param clientTable
|
* @param serviceTable
|
*/
|
public static void serviceToClient(String clientTable,String serviceTable){
|
//1.根据上次同步时间获取需要同步的数据
|
|
//2.保存数据到客户端
|
}
|
/**
|
* 双向同步,本地同步到服务器,服务器同步到本地
|
* @param clientTable
|
* @param serviceTable
|
*/
|
public static void synchroData(String clientTable,String serviceTable){
|
//1.根据上次同步时间和标记未同步条件查找出本地需要同步到服务器端的数据
|
|
//2.将数据发送给服务器,服务器保存后返回需要同步到客户端的数据,同时修改同步时间
|
|
//3.保存数据到客户端
|
}
|
|
}
|