在创建数据库时需要创建多张表,并对他们进行增删改查
一:建立社区里特帮助类,继承SQLiteOpenHelper
public class SqlHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydatabase.db";//库名
private static final int DB_VERSION = 1;//版本
private static final String PRIMATY_KEY = " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT";//主键不为空自动增长
public SqlHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
/** 创建表 */
String sql_user = "create table " + UserInfo_tb.TABLE_NAME + "("
+ UserInfo_tb._ID + " " + PRIMATY_KEY + ","
+ UserInfo_tb.USER_ID + " varchar(20),"
+ UserInfo_tb.USER_PSW + " varchar(20),"
+ UserInfo_tb.USER_REAL_NAME + " varchar(15), )";
String sql_device = "create table " + Device_tb.TABLE_NAME + "("
+ Device_tb._ID + " " + PRIMATY_KEY + ","
+ Device_tb.DEVICE_ID + " varchar(15)," + Device_tb.LAT
+ " double," + Device_tb.LNG + " double ) ";
String sql_fence = " create table " + Fence_tb.TABLE_NAME + "("
+ Fence_tb._ID + " " + PRIMATY_KEY + ","
+ Fence_tb.DEVICE_ID + " varchar(20) ) ";
db.execSQL(sql_user);
db.execSQL(sql_device);
db.execSQL(sql_fence);
}
/* 升级 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class DbProvider extends ContentProvider {
private static final String URI_AUTHORITY = "com.bdyl.user_provider";
private static final int USER_CODE = 1;
private static final int DEVICE_CODE = 2;
private static final int FENCE_CODE = 3;
private SQLiteDatabase db;
/** 三张表的Uri */
public static final Uri URI_USER = Uri.parse("content://" + URI_AUTHORITY
+ "/" + UserInfo_tb.TABLE_NAME);
public static final Uri URI_DEVICE = Uri.parse("content://" + URI_AUTHORITY
+ "/" + Device_tb.TABLE_NAME);
public static final Uri URI_FENCE = Uri.parse("content://" + URI_AUTHORITY
+ "/" + Fence_tb.TABLE_NAME);
private static UriMatcher matcher;// mather类,帮助我们管理识别不同的表
static {// 向matcher里添加三张表的uri(authority,表名(path),code根据传入uri进行匹配,返回的code值来辨识是哪张表)
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(URI_AUTHORITY, UserInfo_tb.TABLE_NAME, USER_CODE);
matcher.addURI(URI_AUTHORITY, Device_tb.TABLE_NAME, DEVICE_CODE);
matcher.addURI(URI_AUTHORITY, Fence_tb.TABLE_NAME, FENCE_CODE);
}
/** 实例化helper 并得到SQLiteDatabase 实例 */
@Override
public boolean onCreate() {
SqlHelper helper = new SqlHelper(getContext());
db = helper.getReadableDatabase();
return false;
}
/** 通过matcher类的match方法来匹配对应的表,对其进行更改 */
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
switch (matcher.match(uri)) {
case USER_CODE:
return db.query(BdylDatabase.UserInfo_tb.TABLE_NAME, projection,
selection, selectionArgs, null, sortOrder, null);
case DEVICE_CODE:
return db.query(BdylDatabase.Device_tb.TABLE_NAME, projection,
selection, selectionArgs, null, sortOrder, null);
case FENCE_CODE:
return db.query(Fence_tb.TABLE_NAME, projection, selection,
selectionArgs, null, sortOrder, null);
}
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long data = 0;
switch (matcher.match(uri)) {
case USER_CODE:
data = db.insert(UserInfo_tb.TABLE_NAME, null, values);
break;
case DEVICE_CODE:
data = db.insert(Device_tb.TABLE_NAME, null, values);
break;
case FENCE_CODE:
data = db.insert(Fence_tb.TABLE_NAME, null, values);
break;
}
return Uri.withAppendedPath(uri, data + "");
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (matcher.match(uri)) {
case USER_CODE:
return db.delete(UserInfo_tb.TABLE_NAME, selection, selectionArgs);
case DEVICE_CODE:
return db.delete(Device_tb.TABLE_NAME, selection, selectionArgs);
case FENCE_CODE:
return db.delete(Fence_tb.TABLE_NAME, selection, selectionArgs);
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
switch (matcher.match(uri)) {
case USER_CODE:
return db.update(BdylDatabase.UserInfo_tb.TABLE_NAME, values,
selection, selectionArgs);
case DEVICE_CODE:
return db.update(Device_tb.TABLE_NAME, values, selection,
selectionArgs);
case FENCE_CODE:
return db.update(Fence_tb.TABLE_NAME, values, selection,
selectionArgs);
}
return 0;
}
}
三: 你可以用这个provider来写一个工具类来对相应表进行操作(如下面)
public class ProvideUtils {
/*
* 更新user表,user=?
*/
public static void updateUser(ContentResolver resolver,
ContentValues values, String user) {
resolver.update(DbProvider.URI_USER, values,
UserInfo_tb.USER_ID + "=?", new String[] { user });
}
/*
* 查询user表 projection信息 user=?
*/
public static Cursor queryUser(ContentResolver resolver,
String[] projection, String user) {
Cursor cursor = resolver.query(DbProvider.URI_USER, projection,
UserInfo_tb.USER_ID + "=?", new String[] { user }, null);
return cursor;
}
/** 设备表 device_tb */
/* user = ? 的所有手表 */
public static Cursor queryDeviceList(ContentResolver resolver,
String[] projection, String userId) {
return resolver.query(DbProvider.URI_DEVICE, projection,
Device_tb.USER_ID + "=?", new String[] { userId }, null);
}
/* device=? 的配置信息 */
public static Cursor queryDeviceInfo(ContentResolver resolver,
String[] projection, String deviceId) {
return resolver.query(DbProvider.URI_DEVICE, projection,
Device_tb.DEVICE_ID + "=?", new String[] { deviceId }, null);
}}
最后千万别忘记在Manifist中注册我们的provider