个人记录
public class DataBaseContentProvider extends ContentProvider { private SQLiteOpenHelper mSQLiteOpenHelper; private SQLiteDatabase db;
/**
* URI
*/
private static final UriMatcher URIMATCHER = new UriMatcher(
UriMatcher.NO_MATCH); private static final int URI_MATCH_USER = 0; private static final int URI_MATCH_FRIEND = 1; private static final int URI_MATCH_TALK = 2; private static final int URI_MATCH_COMMENT = 3; private static final int URI_MATCH_ALARM = 4; static {
URIMATCHER.addURI(Constants.AUTHORITY, FriendTable.TABLE_NAME,
URI_MATCH_FRIEND); URIMATCHER.addURI(Constants.AUTHORITY, UserTable.TABLE_NAME,
URI_MATCH_USER); URIMATCHER.addURI(Constants.AUTHORITY, TalkTable.TABLE_NAME,
URI_MATCH_TALK); URIMATCHER.addURI(Constants.AUTHORITY, CommentTable.TABLE_NAME,
URI_MATCH_COMMENT); URIMATCHER.addURI(Constants.AUTHORITY,AlarmTable.TABLE_NAME,
URI_MATCH_ALARM); } @Override
public boolean onCreate() {
// TODO Auto-generated method stub
mSQLiteOpenHelper = DataBaseHelper.getInstance(getContext());
db = mSQLiteOpenHelper.getWritableDatabase();
return true;
} /**
* 返回要查询的表名
*/
private String getTableNameByUri(Uri uri) {
if (uri == null) {
return null;
}
int match = URIMATCHER.match(uri);
switch (match) {
case URI_MATCH_USER:
return UserTable.TABLE_NAME; case URI_MATCH_FRIEND:
return FriendTable.TABLE_NAME; case URI_MATCH_TALK:
return TalkTable.TABLE_NAME; case URI_MATCH_COMMENT:
return CommentTable.TABLE_NAME; case URI_MATCH_ALARM:
return AlarmTable.TABLE_NAME; default:
throw new IllegalArgumentException(
"DataBaseContentProvider getTableNameByUri");
}
} @Override
public int delete(Uri url, String where, String[] whereArgs) {
// TODO Auto-generated method stub
int match = URIMATCHER.match(url);
if (match == UriMatcher.NO_MATCH) {
throw new IllegalArgumentException("Unknown URL");
} int count = db.delete(getTableNameByUri(url), where, whereArgs);
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(url, null);
return count;
} @Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub switch (URIMATCHER.match(arg0)) {
case URI_MATCH_USER:
return UserTable.CONTENT_TYPE;
case URI_MATCH_FRIEND:
return FriendTable.CONTENT_TYPE;
case URI_MATCH_TALK:
return TalkTable.CONTENT_TYPE;
case URI_MATCH_COMMENT:
return CommentTable.CONTENT_TYPE;
case URI_MATCH_ALARM:
return AlarmTable.CONTENT_TYPE;
default:
throw new IllegalArgumentException(
"DataBaseContentProvider getType");
}
} @Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub int match = URIMATCHER.match(uri);
Uri rtUri = null;
long rowId = db.replace(getTableNameByUri(uri), null, values);
if (rowId > 0) {
rtUri = ContentUris.withAppendedId(uri, rowId);
}
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);
return rtUri;
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
String tableName = this.getTableNameByUri(uri);
Cursor c = db.query(tableName, projection, selection, selectionArgs,
null, null, sortOrder, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
} @Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
String tableName = this.getTableNameByUri(uri);
int rt = db.update(tableName, values, selection, selectionArgs);
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);
return rt;
} }
<provider android:name="com.winning.cqs.pregnantbabycare_cqs_v1.db.DataBaseContentProvider"
android:authorities="com.winning.cqs.pregnantbabycare_cqs_v1"
android:multiprocess="true" >
</provider>
public class User implements Serializable { private static final long serialVersionUID = 1L; public int _id;
public String hospitalHost;
public String hospitalName;
public String lastModify;
public String idNo;
public String mobile;
public String name;
public String hospitalCode;
public String dueDate;
public Integer activity;
public Integer yxtUserID;
public String nick;
public String cardTypeDis;
public Integer hospitalID;
public int id;
public Integer yxtMemberID;
public Integer defaultPatientID;
public String address;
public Integer lastPregnancyID;
public String createDate;
public String cardNo;
public String medicalNo;
public Integer patientID; public static String saveUserToDB(ContentResolver cr, User user) {
if (null == cr || null == user) {
return null;
}
String result = null;
//获取到还没有被修改过的用户信息
User user2 = getUsers(cr, String.valueOf(user.id));
ContentValues values = new ContentValues(); values.put(UserTable.Activity, user.activity);
values.put(UserTable.Address, user.address);
values.put(UserTable.CardNo, user.cardNo);
values.put(UserTable.CardTypeDis, user.cardTypeDis);
values.put(UserTable.CreateDate, user.createDate);
values.put(UserTable.DefaultPatientID, user.defaultPatientID);
values.put(UserTable.DueDate, user.dueDate);
values.put(UserTable.HospitalCode, user.hospitalCode);
values.put(UserTable.HospitalHost, user.hospitalHost);
values.put(UserTable.HospitalID, user.hospitalID);
values.put(UserTable.HospitalName, user.hospitalName);
values.put(UserTable.Id, user.id);
values.put(UserTable.IdNo, user.idNo);
values.put(UserTable.LastModify, user.lastModify);
values.put(UserTable.LastPregnancyID, user.lastPregnancyID);
values.put(UserTable.MedicalNo, user.medicalNo);
values.put(UserTable.Mobile, user.mobile);
values.put(UserTable.Name, user.name);
values.put(UserTable.Nick, user.nick);
values.put(UserTable.PatientID, user.patientID);
values.put(UserTable.YxtMemberID, user.yxtMemberID);
values.put(UserTable.YxtUserID, user.yxtUserID); if (values.size() > 0) {
String whereStr = UserTable.Id + " = " + user.id;
// 查询是否有相同数据
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, whereStr,
null, null);
if ((null != cursor) && (0 < cursor.getCount())) {
cr.update(UserTable.CONTENT_URI, values, whereStr, null);
} else {
cr.insert(UserTable.CONTENT_URI, values);
}
if (cursor != null) {
cursor.close();
}
} if (isUpdate(user, user2))
result = "信息保存成功!"; return result;
}
/**
* 判断用户信息是否被修改
* @param user1
* @param user2
* @return
*/
public static boolean isUpdate(User user1, User user2) {
boolean b = true;
if (user1 == null || user2 == null) {
return b;
}
if (user1.id == user2.id && user1.nick.equals(user2.nick)
&& user1.dueDate.equals(user2.dueDate)) {
b = false;
}
return b;
} public static User getUsers(ContentResolver cr, String id) {
User friend = null;
if (null != cr) {
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, UserTable.Id
+ "=?", new String[] { id }, null);
if (cursor.moveToFirst()) {
friend = new User(); friend._id = cursor
.getInt(cursor.getColumnIndex(UserTable._ID));
friend.activity = cursor.getInt(cursor
.getColumnIndex(UserTable.Activity));
friend.address = cursor.getString(cursor
.getColumnIndex(UserTable.Address));
friend.cardNo = cursor.getString(cursor
.getColumnIndex(UserTable.CardNo));
friend.cardTypeDis = cursor.getString(cursor
.getColumnIndex(UserTable.CardTypeDis));
friend.createDate = cursor.getString(cursor
.getColumnIndex(UserTable.CreateDate));
friend.defaultPatientID = cursor.getInt(cursor
.getColumnIndex(UserTable.DefaultPatientID));
friend.dueDate = cursor.getString(cursor
.getColumnIndex(UserTable.DueDate));
friend.hospitalCode = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalCode));
friend.hospitalHost = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalHost));
friend.hospitalID = cursor.getInt(cursor
.getColumnIndex(UserTable.HospitalID));
friend.hospitalName = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalName));
friend.id = cursor.getInt(cursor.getColumnIndex(UserTable.Id));
friend.idNo = cursor.getString(cursor
.getColumnIndex(UserTable.IdNo));
friend.lastModify = cursor.getString(cursor
.getColumnIndex(UserTable.LastModify));
friend.lastPregnancyID = cursor.getInt(cursor
.getColumnIndex(UserTable.LastPregnancyID));
friend.medicalNo = cursor.getString(cursor
.getColumnIndex(UserTable.MedicalNo));
friend.mobile = cursor.getString(cursor
.getColumnIndex(UserTable.Mobile));
friend.name = cursor.getString(cursor
.getColumnIndex(UserTable.Name));
friend.nick = cursor.getString(cursor
.getColumnIndex(UserTable.Nick));
friend.patientID = cursor.getInt(cursor
.getColumnIndex(UserTable.PatientID));
friend.yxtMemberID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtMemberID));
friend.yxtUserID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtUserID)); cursor.moveToNext();
cursor.close();
} }
return friend;
} public static ArrayList<User> getAllUsers(ContentResolver cr) {
ArrayList<User> users = new ArrayList<User>();
if (null != cr) {
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, null, null,
null);
if (null != cursor) {
int friendsCount = cursor.getCount(); cursor.moveToFirst();
for (int k = 0; k < friendsCount; k++) {
User friend = new User(); friend._id = cursor.getInt(cursor
.getColumnIndex(UserTable._ID));
friend.activity = cursor.getInt(cursor
.getColumnIndex(UserTable.Activity));
friend.address = cursor.getString(cursor
.getColumnIndex(UserTable.Address));
friend.cardNo = cursor.getString(cursor
.getColumnIndex(UserTable.CardNo));
friend.cardTypeDis = cursor.getString(cursor
.getColumnIndex(UserTable.CardTypeDis));
friend.createDate = cursor.getString(cursor
.getColumnIndex(UserTable.CreateDate));
friend.defaultPatientID = cursor.getInt(cursor
.getColumnIndex(UserTable.DefaultPatientID));
friend.dueDate = cursor.getString(cursor
.getColumnIndex(UserTable.DueDate));
friend.hospitalCode = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalCode));
friend.hospitalHost = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalHost));
friend.hospitalID = cursor.getInt(cursor
.getColumnIndex(UserTable.HospitalID));
friend.hospitalName = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalName));
friend.id = cursor.getInt(cursor
.getColumnIndex(UserTable.Id));
friend.idNo = cursor.getString(cursor
.getColumnIndex(UserTable.IdNo));
friend.lastModify = cursor.getString(cursor
.getColumnIndex(UserTable.LastModify));
friend.lastPregnancyID = cursor.getInt(cursor
.getColumnIndex(UserTable.LastPregnancyID));
friend.medicalNo = cursor.getString(cursor
.getColumnIndex(UserTable.MedicalNo));
friend.mobile = cursor.getString(cursor
.getColumnIndex(UserTable.Mobile));
friend.name = cursor.getString(cursor
.getColumnIndex(UserTable.Name));
friend.nick = cursor.getString(cursor
.getColumnIndex(UserTable.Nick));
friend.patientID = cursor.getInt(cursor
.getColumnIndex(UserTable.PatientID));
friend.yxtMemberID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtMemberID));
friend.yxtUserID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtUserID)); users.add(friend); cursor.moveToNext();
}
cursor.close();
} }
return users;
} public static class UserTable implements BaseColumns {
public static final String TABLE_NAME = "user"; public static final Uri CONTENT_URI = Uri.parse("content://"
+ Constants.AUTHORITY + "/" + TABLE_NAME); public static final String CONTENT_TYPE = "vnd.android.cursor.dir/"
+ Constants.AUTHORITY + "/" + TABLE_NAME; public static final String _ID = "_id";
public static final String HospitalHost = "hospitalHost";
public static final String HospitalName = "hospitalName";
public static final String LastModify = "lastModify";
public static final String IdNo = "idNo";
public static final String Mobile = "mobile";
public static final String Name = "name";
public static final String HospitalCode = "hospitalCode";
public static final String DueDate = "dueDate";
public static final String Activity = "activity";
public static final String YxtUserID = "yxtUserID";
public static final String Nick = "nick";
public static final String CardTypeDis = "cardTypeDis";
public static final String HospitalID = "hospitalID";
public static final String Id = "id";
public static final String YxtMemberID = "yxtMemberID";
public static final String DefaultPatientID = "defaultPatientID";
public static final String Address = "address";
public static final String LastPregnancyID = "lastPregnancyID";
public static final String CreateDate = "createDate";
public static final String CardNo = "cardNo";
public static final String MedicalNo = "medicalNo";
public static final String PatientID = "patientID"; public static final String TABLE_CREATESQL = "CREATE TABLE "
+ TABLE_NAME + " (" + "_id"
+ " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + HospitalHost
+ " TEXT," + HospitalName + " TEXT," + LastModify + " TEXT,"
+ IdNo + " TEXT," + Mobile + " TEXT," + Name + " TEXT,"
+ HospitalCode + " TEXT," + DueDate + " TEXT," + Activity
+ " TEXT," + YxtUserID + " TEXT," + Nick + " TEXT,"
+ CardTypeDis + " TEXT," + HospitalID + " TEXT," + Id
+ " TEXT," + YxtMemberID + " TEXT," + DefaultPatientID
+ " TEXT," + Address + " TEXT," + LastPregnancyID + " TEXT,"
+ CreateDate + " TEXT," + CardNo + " TEXT," + MedicalNo
+ " TEXT," + PatientID + " INTEGER" + ");"; } }