package tv.acfun.im.support.db;
import java.util.ArrayList;
import java.util.List;
import tv.acfun.im.support.bean.ContactBean;
import tv.acfun.im.support.bean.GroupBean;
import tv.acfun.im.support.bean.GroupContactBean;
import tv.acfun.im.support.bean.MessageBean;
import tv.acfun.im.support.bean.SessionBean;
import tv.acfun.im.util.AppLogger;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
private static final String TAG = "DbHelper";
private final static int DB_VERSION = 1;
private final static String DB_NAME = "acFun.db";
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
private static DbHelper db;
public static DbHelper getInstance(Context mContex) {
if (db == null) {
synchronized (DbHelper.class) {
if (db == null) {
db = new DbHelper(mContex);
}
}
}
return db;
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建信息表
String createMsgSql = "create table Message(msg_id text primary key,session_id text,msg_from text,msg_to text,msg_type integer,"
+ "state integer,send_time text,send_nickname text,user_id text not null,msg_content text,content_type integer,send_icon text)";
// 创建会话表
String createSessionSql = "create table Session(id integer primary key autoincrement,session_id text,pic_url text,dis_name text,type integer,unread_count integer,msg_content text,msg_time text,msg_state integer,isreceive integer default 1,isremoved integer default 0,is_top_time text,user_id text not null)";
// 创建联系人表
String createContactSql = "create table Contact(uid text,nick_name text,age integer,pic_url text,gender integer,piniyin text,user_id text not null,constellation text,area text)";
// 创建群组表
String createGroupSql = "create table acfun_group(groupid integer primary key,pic_url text,group_name text,member integer,desci text,creater text,create_time text,user_id text not null)";
// 创建群成员关系表
String createGroupContact = "create table Group_Contact(id integer primary key autoincrement,user_id text not null,uid text,groupid integer,FOREIGN KEY(groupid) REFERENCES acfun_group(groupid))";
/*-------------------------资源评论表-----------------------*/
String createCommentSql = "create table Comment(id integer primary key autoincrement,comment_id integer,user_id integer,resource_id integer)";
/*-------------------------------------------------------*/
/*-------------------------收藏表--------服务端有返回。。其实不用本地记录的 ---------------*/
String createLikeSql = "create table Like(id integer primary key autoincrement,user_id integer,resource_id integer)";
/*-------------------------------------------------------*/
/*-------------------------关注表--------服务端有返回。。其实不用本地记录的 ----------------*/
String createCareSql = "create table Care(id integer primary key autoincrement,user_id integer,care_user_id integer)";
/*-------------------------------------------------------*/
db.execSQL(createMsgSql);
db.execSQL(createSessionSql);
db.execSQL(createContactSql);
db.execSQL(createGroupSql);
db.execSQL(createGroupContact);
db.execSQL(createCommentSql);
db.execSQL(createLikeSql);
db.execSQL(createCareSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists Message";
String deleteSessionSql = "drop table if exists Session";
String deleteContact = "drop table if exists Contact";
String deleteGroup = "drop table if exists acfun_group";
String deleteGroupContact = "drop table if exists Group_Contact";
db.execSQL(sql);
db.execSQL(deleteSessionSql);
db.execSQL(deleteContact);
db.execSQL(deleteGroup);
db.execSQL(deleteGroupContact);
onCreate(db);
}
/**
* 判断是否关注了此人
*
* @param user_id
* @param care_user_id
* @return
*/
public boolean isCare(int user_id, int care_user_id) {
boolean isCare = false;
String sql = "select * from Care where user_id = ? and care_user_id = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] {
String.valueOf(user_id), String.valueOf(care_user_id) });
if (cursor.getCount() > 0) {
isCare = true;
}
if (db != null) {
db.close();
}
if (cursor != null) {
cursor.close();
}
return isCare;
}
/**
* 关注 保存到本地
*
* @param user_id
* @param care_user_id
*/
public void insertCareToSql(int user_id, int care_user_id) {
String sql = "insert into Care(user_id,care_user_id) values(?,?)";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { user_id, care_user_id });
db.close();
}
/**
* 取消关注此人 从本地删除
*
* @param user_id
* @param care_user_id
*/
public void delCareFromSql(int user_id, int care_user_id) {
String sql = "delete from Care where user_id=? and care_user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { user_id, care_user_id });
db.close();
}
/**
* 判断是否收藏此资源
*
* @param user_id
* @param resouce_id
* @return
*/
public boolean isLike(int user_id, int resouce_id) {
boolean isLike = false;
String sql = "select * from Like where user_id = ? and resource_id = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] {
String.valueOf(user_id), String.valueOf(resouce_id) });
if (cursor.getCount() > 0) {
isLike = true;
}
if (db != null) {
db.close();
}
if (cursor != null) {
cursor.close();
}
return isLike;
}
/**
* 收藏 插入一条资源到本地
*
* @param user_id
* @param sources_id
*/
public void insertLikeToSql(int user_id, int sources_id) {
String sql = "insert into Like(user_id,resource_id) values(?,?)";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { user_id, sources_id });
db.close();
}
/**
* 取消收藏 从本地删除记录
*
* @param user_id
* @param sources_id
*/
public void delLikeFromSql(int user_id, int sources_id) {
String sql = "delete from Like where user_id=? and resource_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { user_id, sources_id });
db.close();
}
/**
* 查询所有我点赞过的评论
*
* @param user_id
* @param resouce_id
* @return
*/
public int[] getCommentsIds(int user_id, int resouce_id) {
String sql = "select comment_id from Comment where user_id = ? and resource_id = ?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] {
String.valueOf(user_id), String.valueOf(resouce_id) });
int[] arrys = new int[cursor.getCount()];
try {
cursor.moveToFirst();
cursor.moveToPrevious();
int index = 0;
while (cursor.moveToNext()) {
arrys[index] = cursor.getInt(cursor
.getColumnIndex("comment_id"));
index++;
}
AppLogger.d(TAG, "query comment from sqlite sucess !");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
db.close();
}
if (cursor != null) {
cursor.close();
}
}
return arrys;
}
/**
* 插入一条攒过的评论到数据库
*
* @param user_id
* @param sources_id
* @param comment_id
*/
public void insertCommmentIdToSql(int user_id, int sources_id,
int comment_id) {
String sql = "insert into Comment(user_id,resource_id,comment_id) values(?,?,?)";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { user_id, sources_id, comment_id, });
db.close();
AppLogger.d(TAG, "insert comment to sqlite sucess !commenId = "
+ comment_id);
}
/**
* 取消赞这个评论
*
* @param user_id
* @param sources_id
* @param comment_id
*/
public void delCommentFromSql(int user_id, int sources_id, int comment_id) {
String sql = "delete from Comment where comment_id=? and user_id=? and resource_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { comment_id, user_id, sources_id });
db.close();
AppLogger.d(TAG, "delet comment from sqlite sucess !commenId = "
+ comment_id);
}
// 删除多条消息记录
public void deleteMutiapleMessages(List<MessageBean> list, String user_id) {
String sql = "delete from Message where msg_id=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getMsg_id(), user_id });
}
db.close();
}
// 删除模块,根据消息ID删除一条Message
public void deleteMessageByMsgId(int id, String user_id) {
String sql = "delete from Message where msg_id=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { id, user_id });
db.close();
}
// 根据会话ID删除一条回话
public void deleteSessionById(int id, String user_id) {
String sql = "delete from Session where id=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { id, user_id });
db.close();
}
// 删除多条会话
public void deleteSessions(List<SessionBean> list, String user_id) {
String sql = "delete from Session where id=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getId(), user_id });
}
db.close();
}
// 根据Id更新一条回话记录是否被删除的状态以及未读数量
// isRemoved 1为删除,0为没有删除
public void updateSessionToDeleteState(int isRemoved, int count, int id,
String user_id) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "update Session set is_removed=?,unread_count=? where id=? and user_id=?";
db.execSQL(sql, new Object[] { isRemoved, count, id, user_id });
db.close();
}
// 跟新回话列表里面的置顶时间
public void updateTopTime(String time, int id, String user_id) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "update Session set is_top_time=? where id=? and user_id=?";
db.execSQL(sql, new Object[] { time, id, user_id });
db.close();
}
// 根据uid删除联系人里面的一条记录,并且删除群成员里面的和我是好友的字段
public void deleteContactById(int id, String user_id) {
String sql = "delete from Contact where id=? and user_id=?";
// 根据Id删除群成员里面的好友记录
String updatgeMemberSql = "delete uid from Group_Contact where uid=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, new Object[] { id, user_id });
db.execSQL(updatgeMemberSql, new Object[] { id, user_id });
db.close();
}
// 根据uid删除联系人里面的多条记录,并且删除群成员里面的和我是好友的字段
public void deleteContactS(List<ContactBean> list) {
String sql = "delete from Contact where id=?";
// 根据Id删除群成员里面的好友记录
String updatgeMemberSql = "delete uid from Group_Contact where uid=? and user_id=?";
SQLiteDatabase db = this.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getUid() });
db.execSQL(updatgeMemberSql, new Object[] { list.get(i).getUid(),
list.get(i).getUser_id() });
}
db.close();
}
// 根据groupId删除群组表里面的一条记录,并且删除与之相关的群成员
public void deleteCroupById(int groupId, String user_id) {
String sql = "delete from acfun_group where groupid=? and user_id=?";
String sqlGroupMemeber = "delete from Group_Contact where groupid=? and user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(sql, new Object[] { groupId, user_id });
db.execSQL(sqlGroupMemeber, new Object[] { groupId, user_id });
db.close();
}
// 根据群成员ID删除一条群成员记录,也就是把一个群成员踢出群。
public void deleteMemberById(int id, String user_id) {
String sql = "delete from Group_Contact where id=? and user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(sql, new Object[] { id, user_id });
db.close();
}
// 删除多条群成员记录
public void deleteMembers(List<GroupContactBean> list) {
String sql = "delete from Group_Contact where id=? and user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getId(),
list.get(i).getUser_id() });
}
db.close();
}
// 获取所有群成员关系数据
public List<GroupContactBean> getGroupContactBeans(String groupid,
String user_id) {
List<GroupContactBean> list = new ArrayList<GroupContactBean>();
String sql = "select * from Group_Contact wehre groupid=? and user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] { groupid, user_id });
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
GroupContactBean gcb = new GroupContactBean();
gcb.setId(cursor.getInt(cursor.getColumnIndex("id")));
gcb.setUser_id(cursor.getString(cursor
.getColumnIndex("user_id")));
gcb.setGroupId(cursor.getString(cursor
.getColumnIndex("groupid")));
gcb.setUid(cursor.getString(cursor.getColumnIndex("uid")));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 获取所有小组列表信息
public List<GroupBean> getGroupBeans(String user_id) {
List<GroupBean> list = new ArrayList<GroupBean>();
String sql = "select * from acfun_group wher user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] { user_id });
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
GroupBean gb = new GroupBean();
gb.setGroupId(cursor.getInt(cursor.getColumnIndex("groupid")));
gb.setPic_url(cursor.getString(cursor.getColumnIndex("pic_url")));
gb.setGroup_name(cursor.getString(cursor
.getColumnIndex("group_name")));
gb.setMember(cursor.getString(cursor.getColumnIndex("member")));
gb.setDesc(cursor.getString(cursor.getColumnIndex("desci")));
gb.setCreater(cursor.getString(cursor.getColumnIndex("creater")));
gb.setCreatTime(cursor.getString(cursor
.getColumnIndex("create_time")));
gb.setUserId(cursor.getString(cursor.getColumnIndex("user_id")));
list.add(gb);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 获取所有联系人列表
public List<ContactBean> getContactBeans(String user_id) {
List<ContactBean> list = new ArrayList<ContactBean>();
String sql = "select * from Contact where user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] { user_id });
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
ContactBean cb = new ContactBean();
cb.setUid(cursor.getString(cursor.getColumnIndex("uid")));
cb.setNick_name(cursor.getString(cursor
.getColumnIndex("nick_name")));
cb.setAge(cursor.getInt(cursor.getColumnIndex("age")));
cb.setPic_url(cursor.getString(cursor.getColumnIndex("pic_url")));
cb.setGender(cursor.getInt(cursor.getColumnIndex("gender")));
cb.setPinyin(cursor.getString(cursor.getColumnIndex("piniyin")));
cb.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
cb.setArea(cursor.getString(cursor.getColumnIndex("area")));
cb.setConstellation(cursor.getString(cursor
.getColumnIndex("constellation")));
list.add(cb);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 分页获取回话;count:一次返回多少条数据,endPage:从第几条之后开始往后取
public List<MessageBean> getPageMessage(int count, int endPage,
String sessionId, String user_id) {
List<MessageBean> list = new ArrayList<MessageBean>();
String sql = "select * from (select * from Message order by send_time desc) where session_id=? and user_id=? limit ? OFFSET ? ";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] { sessionId, user_id,
String.valueOf(count), String.valueOf(endPage) });
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
MessageBean mb = new MessageBean();
mb.setMsg_id(cursor.getString(0));
mb.setSessionId(cursor.getString(1));
mb.setMsg_from(cursor.getString(2));
mb.setMsg_to(cursor.getString(3));
mb.setMsg_type(cursor.getInt(cursor.getColumnIndex("type")));
mb.setState(cursor.getInt(cursor.getColumnIndex("state")));
mb.setSend_time(cursor.getString(cursor
.getColumnIndex("send_time")));
mb.setSend_nickname(cursor.getString(cursor
.getColumnIndex("send_nickname")));
mb.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
mb.setMsg_content(cursor.getString(cursor
.getColumnIndex("msg_content")));
list.add(mb);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 获取所有会话列表
public List<SessionBean> getSessionList(String user_id) {
List<SessionBean> list = new ArrayList<SessionBean>();
String sql = "select * from Session where user_id=?";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, new String[] { user_id });
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
SessionBean sb = new SessionBean();
sb.setId(cursor.getInt(cursor.getColumnIndex("id")));
sb.setSessionId(cursor.getString(cursor
.getColumnIndex("session_id")));
sb.setPic_url(cursor.getString(cursor.getColumnIndex("pic_url")));
sb.setDis_name(cursor.getString(cursor
.getColumnIndex("dis_name")));
sb.setType(cursor.getInt(cursor.getColumnIndex("type")));
sb.setUnread_count(cursor.getInt(cursor
.getColumnIndex("unread_count")));
sb.setMsg_content(cursor.getString(cursor
.getColumnIndex("msg_content")));
sb.setMsg_time(cursor.getString(cursor
.getColumnIndex("msg_time")));
sb.setMsg_state(cursor.getInt(cursor
.getColumnIndex("msg_state")));
sb.setIsReceived(cursor.getInt(cursor
.getColumnIndex("isreceive")));
sb.setIsRemoved(cursor.getInt(cursor
.getColumnIndex("isremoved")));
sb.setIs_top_time(cursor.getString(cursor
.getColumnIndex("is_top_time")));
sb.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
list.add(sb);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 获取所有消息列表List
public List<MessageBean> getMessageList() {
List<MessageBean> list = new ArrayList<MessageBean>();
String sql = "select * from Message";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
try {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
MessageBean mb = new MessageBean();
mb.setMsg_id(cursor.getString(0));
mb.setSessionId(cursor.getString(1));
mb.setMsg_from(cursor.getString(2));
mb.setMsg_to(cursor.getString(3));
mb.setMsg_type(cursor.getInt(cursor.getColumnIndex("msg_type")));
mb.setState(cursor.getInt(cursor.getColumnIndex("state")));
mb.setSend_time(cursor.getString(cursor
.getColumnIndex("send_time")));
mb.setSend_nickname(cursor.getString(cursor
.getColumnIndex("send_nickname")));
mb.setUser_id(cursor.getString(cursor.getColumnIndex("user_id")));
mb.setMsg_content(cursor.getString(cursor
.getColumnIndex("msg_content")));
mb.setContent_type(cursor.getInt(cursor
.getColumnIndex("content_type")));
mb.setSendIcon(cursor.getString(cursor
.getColumnIndex("send_icon")));
list.add(mb);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
cursor.close();
db.close();
}
return list;
}
// 向群成员表里面插入数据
public void insertInToGroupContact(GroupContactBean gcb) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into Group_Contact(user_id,groupid,uid) values(?,?,?)";
db.execSQL(
sql,
new Object[] { gcb.getUser_id(), gcb.getGroupId(), gcb.getUid() });
db.close();
}
// 向群成员表里面插入多条数据
public void insertMutipleGroupContactS(List<GroupContactBean> list) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into Group_Contact(user_id,groupid,uid) values(?,?,?)";
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getUid(),
list.get(i).getGroupId(), list.get(i).getUid() });
}
db.close();
}
// 向群组表里面插入数据
public void insertInToGroup(GroupBean gb) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into acfun_group(pic_url,group_name,member,desci,creater,create_time,user_id) values(?,?,?,?,?,?,?)";
db.execSQL(
sql,
new Object[] { gb.getPic_url(), gb.getGroup_name(),
gb.getMember(), gb.getDesc(), gb.getCreater(),
gb.getCreatTime(), gb.getUserId() });
db.close();
}
// 向联系人表里面插入数据
public void insertInToContact(ContactBean cb) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into Contact(uid,nick_name,age,pic_url,gender,piniyin,user_id,constellation,area) values(?,?,?,?,?,?,?,?,?)";
db.execSQL(
sql,
new Object[] { cb.getUid(), cb.getNick_name(), cb.getAge(),
cb.getPic_url(), cb.getGender(), cb.getPinyin(),
cb.getUser_id(), cb.getConstellation(), cb.getArea() });
db.close();
}
// 插入一条Message;
public void insertIntoMsg(MessageBean msg) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into Message(msg_id,session_id,msg_from,msg_to,msg_type,state,send_time,send_nickname,user_id,content_type,send_icon) values(?,?,?,?,?,?,?,?,?,?,?)";
db.execSQL(
sql,
new Object[] { msg.getMsg_id(), msg.getSessionId(),
msg.getMsg_from(), msg.getMsg_to(), msg.getMsg_type(),
msg.getState(), msg.getSend_time(), msg.getUser_id(),
msg.getContent_type(), msg.getSendIcon() });
db.close();
}
// 插入多条Message;
public void insertMutipleMsg(List<MessageBean> list) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "insert into Message(msg_id,session_id,msg_from,msg_to,msg_type,state,send_time,send_nickname,user_id,content_type,msg_content,send_icon) values(?,?,?,?,?,?,?,?,?,?,?,?)";
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getMsg_id(),
list.get(i).getSessionId(), list.get(i).getMsg_from(),
list.get(i).getMsg_to(), list.get(i).getMsg_type(),
list.get(i).getState(), list.get(i).getSend_time(),
list.get(i).getSend_nickname(), list.get(i).getUser_id(),
list.get(i).getContent_type(),
list.get(i).getMsg_content(), list.get(i).getSendIcon() });
}
db.close();
}
// 插入一条回话
public void insertInToSession(SessionBean s) {
String sql = "insert into Session(session_id,pic_url,dis_name,type,unread_count,msg_content,msg_time,msg_state,isreceive,isremoved,is_top_time,user_id) values(?,?,?,?,?,?,?,?,?,?,?,?)";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(
sql,
new Object[] { s.getSessionId(), s.getPic_url(),
s.getDis_name(), s.getType(), s.getUnread_count(),
s.getMsg_content(), s.getMsg_time(), s.getMsg_state(),
s.getIsReceived(), s.getIsRemoved(),
s.getIs_top_time(), s.getUser_id() });
db.close();
}
// 插入多条回话
public void insertMutipleSession(List<SessionBean> list) {
String sql = "insert into Session(session_id,pic_url,dis_name,type,unread_count,msg_content,msg_time,msg_state,isreceive,isremoved,is_top_time,user_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
SQLiteDatabase db = this.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
db.execSQL(sql, new Object[] { list.get(i).getSessionId(),
list.get(i).getPic_url(), list.get(i).getType(),
list.get(i).getUnread_count(),
list.get(i).getMsg_content(), list.get(i).getMsg_time(),
list.get(i).getMsg_state(), list.get(i).getIsReceived(),
list.get(i).getIsRemoved(), list.get(i).getIs_top_time(),
list.get(i).getUser_id() });
}
db.close();
}
// 删除所有表里面的数据
public void deleteAllTable() {
SQLiteDatabase db = this.getWritableDatabase();
String deleteMsgSql = "delete from Message";
String deleteSessionSql = "delete from Session";
String deleteContactSql = "delete from Contact";
String deleteGroupSql = "delete from acfun_group";
String deleteGroupContactSql = "delete from Group_Contact";
String deleteCommentSql = "delete from Comment";
String deleteLikeSql = "delete from Like";
String deleteCareSql = "delete from Care";
db.execSQL(deleteMsgSql);
db.execSQL(deleteSessionSql);
db.execSQL(deleteContactSql);
db.execSQL(deleteGroupSql);
db.execSQL(deleteGroupContactSql);
db.execSQL(deleteCommentSql);
db.execSQL(deleteLikeSql);
db.execSQL(deleteCareSql);
db.close();
}
}