1。创建凡信的数据库的 系统
SQLiteOpenHelper 类的基本用法是:当需要创建或打开一个数据库并获得数据库对象时,首先根据指定的文件名创建一个辅助对象,然后调用该对象的getWritableDatabase 或 getReadableDatabase方法 获得SQLiteDatabase 对象。
package com.fanxin.app.domain;
import com.easemob.chat.EMContact;
public class User extends EMContact {
private int unreadMsgCount;
private String header;
private String usernick;
private String sex;
private String tel;
private String fxid;
private String region;
private String avatar;
private String sign;
private String beizhu;
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public int getUnreadMsgCount() {
return unreadMsgCount;
}
public void setUnreadMsgCount(int unreadMsgCount) {
this.unreadMsgCount = unreadMsgCount;
}
public void setNick(String usernick){
this.usernick=usernick;
}
public String getNick(){
return usernick;
}
public void setAvatar(String avatar){
this.avatar=avatar;
}
public String getAvatar(){
return avatar;
}
public void setTel(String tel){
this.tel=tel;
}
public String getTel(){
return tel;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getSign() {
return sign;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setFxid(String fxid) {
this.fxid = fxid;
}
public String getFxid() {
return fxid;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegion() {
return region;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public String getBeizhu() {
return beizhu;
}
@Override
public int hashCode() {
return 17 * getUsername().hashCode();
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof User)) {
return false;
}
return getUsername().equals(((User) o).getUsername());
}
@Override
public String toString() {
return username;
}
}
/**
* Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fanxin.app.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.fanxin.app.fx.others.TopUserDao;
import com.fanxin.applib.controller.HXSDKHelper;
public class DbOpenHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static DbOpenHelper instance;
private static final String TOPUSER_TABLE_CREATE = "CREATE TABLE "
+ TopUserDao.TABLE_NAME + " ("
+ TopUserDao.COLUMN_NAME_TIME +" TEXT, "
+ TopUserDao.COLUMN_NAME_IS_GOUP +" TEXT, "
+ TopUserDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY);";
private static final String USERNAME_TABLE_CREATE = "CREATE TABLE "
+ UserDao.TABLE_NAME + " ("
+ UserDao.COLUMN_NAME_NICK +" TEXT, "
+ UserDao.COLUMN_NAME_AVATAR +" TEXT, "
+ UserDao.COLUMN_NAME_BEIZHU +" TEXT, "
+ UserDao.COLUMN_NAME_FXID +" TEXT, "
+ UserDao.COLUMN_NAME_REGION +" TEXT, "
+ UserDao.COLUMN_NAME_SEX +" TEXT, "
+ UserDao.COLUMN_NAME_SIGN +" TEXT, "
+ UserDao.COLUMN_NAME_TEL +" TEXT, "
+ UserDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY);";
private static final String INIVTE_MESSAGE_TABLE_CREATE = "CREATE TABLE "
+ InviteMessgeDao.TABLE_NAME + " ("
+ InviteMessgeDao.COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ InviteMessgeDao.COLUMN_NAME_FROM + " TEXT, "
+ InviteMessgeDao.COLUMN_NAME_GROUP_ID + " TEXT, "
+ InviteMessgeDao.COLUMN_NAME_GROUP_Name + " TEXT, "
+ InviteMessgeDao.COLUMN_NAME_REASON + " TEXT, "
+ InviteMessgeDao.COLUMN_NAME_STATUS + " INTEGER, "
+ InviteMessgeDao.COLUMN_NAME_ISINVITEFROMME + " INTEGER, "
+ InviteMessgeDao.COLUMN_NAME_TIME + " TEXT); ";
private DbOpenHelper(Context context) {
super(context, getUserDatabaseName(), null, DATABASE_VERSION);
}
public static DbOpenHelper getInstance(Context context) {
if (instance == null) {
instance = new DbOpenHelper(context.getApplicationContext());
}
return instance;
}
private static String getUserDatabaseName() {
return HXSDKHelper.getInstance().getHXId() + "_glufine.db";
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(USERNAME_TABLE_CREATE);
db.execSQL(INIVTE_MESSAGE_TABLE_CREATE);
db.execSQL(TOPUSER_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void closeDB() {
if (instance != null) {
try {
SQLiteDatabase db = instance.getWritableDatabase();
db.close();
} catch (Exception e) {
e.printStackTrace();
}
instance = null;
}
}
}
package com.fanxin.app.db;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import com.fanxin.app.Constant;
import com.fanxin.app.domain.User;
import com.easemob.util.HanziToPinyin;
@SuppressLint("DefaultLocale")
public class UserDao {
public static final String TABLE_NAME = "uers";
public static final String COLUMN_NAME_ID = "username";
public static final String COLUMN_NAME_NICK = "nick";
public static final String COLUMN_NAME_SEX = "sex";
public static final String COLUMN_NAME_AVATAR = "avatar";
public static final String COLUMN_NAME_SIGN = "sign";
public static final String COLUMN_NAME_TEL = "tel";
public static final String COLUMN_NAME_FXID = "fxid";
public static final String COLUMN_NAME_REGION = "region";
public static final String COLUMN_NAME_BEIZHU = "beizhu";
public static final String COLUMN_NAME_IS_STRANGER = "is_stranger";
private DbOpenHelper dbHelper;
public UserDao(Context context) {
dbHelper = DbOpenHelper.getInstance(context);
}
/**
* 保存好友list
*
* @param contactList
*/
public void saveContactList(List<User> contactList) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
if (db.isOpen()) {
db.delete(TABLE_NAME, null, null);
for (User user : contactList) {
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_ID, user.getUsername());
if (user.getNick() != null) {
values.put(COLUMN_NAME_NICK, user.getNick());
}
if (user.getBeizhu() != null) {
values.put(COLUMN_NAME_BEIZHU, user.getBeizhu());
}
if (user.getTel() != null) {
values.put(COLUMN_NAME_TEL, user.getTel());
}
if (user.getSex() != null) {
values.put(COLUMN_NAME_SEX, user.getSex());
}
if (user.getAvatar() != null) {
values.put(COLUMN_NAME_AVATAR, user.getAvatar());
}
if (user.getSign() != null) {
values.put(COLUMN_NAME_SIGN, user.getSign());
}
if (user.getFxid() != null) {
values.put(COLUMN_NAME_FXID, user.getFxid());
}
if (user.getRegion()!= null) {
values.put(COLUMN_NAME_REGION, user.getRegion());
}
db.replace(TABLE_NAME, null, values);
}
}
}
/**
* 获取好友list
*
* @return
*/
@SuppressLint("DefaultLocale")
public Map<String, User> getContactList() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Map<String, User> users = new HashMap<String, User>();
if (db.isOpen()) {
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME /* + " desc" */, null);
while (cursor.moveToNext()) {
String username = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_ID));
String nick = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_NICK));
String avatar = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_AVATAR));
String tel = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_TEL));
String sign = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_SIGN));
String sex = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_SEX));
String region = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_REGION));
String beizhu = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_BEIZHU));
String fxid = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_FXID));
User user = new User();
user.setUsername(username);
user.setNick(nick);
user.setBeizhu(beizhu);
user.setFxid(fxid);
user.setRegion(region);
user.setSex(sex);
user.setSign(sign);
user.setTel(tel);
user.setAvatar(avatar);
String headerName = null;
if (!TextUtils.isEmpty(user.getNick())) {
headerName = user.getNick();
} else {
headerName = user.getUsername();
}
if (username.equals(Constant.NEW_FRIENDS_USERNAME) || username.equals(Constant.GROUP_USERNAME)) {
user.setHeader("");
} else if (Character.isDigit(headerName.charAt(0))) {
user.setHeader("#");
} else {
user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1))
.get(0).target.substring(0, 1).toUpperCase());
char header = user.getHeader().toLowerCase().charAt(0);
if (header < 'a' || header > 'z') {
user.setHeader("#");
}
}
users.put(username, user);
}
cursor.close();
}
return users;
}
/**
* 删除一个联系人
* @param username
*/
public void deleteContact(String username){
SQLiteDatabase db = dbHelper.getWritableDatabase();
if(db.isOpen()){
db.delete(TABLE_NAME, COLUMN_NAME_ID + " = ?", new String[]{username});
}
}
/**
* 保存一个联系人
* @param user
*/
public void saveContact(User user){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_ID, user.getUsername());
if (user.getNick() != null) {
values.put(COLUMN_NAME_NICK, user.getNick());
}
if (user.getBeizhu() != null) {
values.put(COLUMN_NAME_BEIZHU, user.getBeizhu());
}
if (user.getTel() != null) {
values.put(COLUMN_NAME_TEL, user.getTel());
}
if (user.getSex() != null) {
values.put(COLUMN_NAME_SEX, user.getSex());
}
if (user.getAvatar() != null) {
values.put(COLUMN_NAME_AVATAR, user.getAvatar());
}
if (user.getSign() != null) {
values.put(COLUMN_NAME_SIGN, user.getSign());
}
if (user.getFxid() != null) {
values.put(COLUMN_NAME_FXID, user.getFxid());
}
if (user.getRegion()!= null) {
values.put(COLUMN_NAME_REGION, user.getRegion());
}
if(db.isOpen()){
db.replace(TABLE_NAME, null, values);
}
}
}