Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过ContentResolver读取SQLite时使用JOIN_Android_Sqlite - Fatal编程技术网

Android 通过ContentResolver读取SQLite时使用JOIN

Android 通过ContentResolver读取SQLite时使用JOIN,android,sqlite,Android,Sqlite,在android中使用ContentResolver时,如何连接两个表? 现在,我可以通过以下方式访问用户表: contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI, DatabaseTables.USERS); selection = DatabaseColumns.USER_ID + " = " + String.valueOf(userId); cursor = context.getCont

在android中使用ContentResolver时,如何连接两个表? 现在,我可以通过以下方式访问用户表:

    contentUri = Uri.withAppendedPath(ContentProviderDB.CONTENT_URI, DatabaseTables.USERS);
    selection = DatabaseColumns.USER_ID + " = " + String.valueOf(userId);
    cursor = context.getContentResolver().query(contentUri, null, selection, null, null);
但我如何加入用户产品,例如?据我所知,“query”方法不支持连接操作

在android中使用ContentResolver时,如何连接两个表

你没有。您的
ContentProvider
需要公开一些表示
JOIN
的虚拟“表”


这让人想起REST风格的Web服务。此类服务的客户端仅限于服务公开的特定内容。客户机不能在这样的Web服务上直接表示
连接
,除非服务本身有一个准备好的接口来执行这样的
连接

,如果您仍然想知道,这里有一个自定义内容提供程序的示例,它连接了表2。注意
查询中的投影
,您需要为“\u id”设置别名。如果只为“_id”进行投影,则会出现ambiguse列异常,因为有两个这样的列,如果不进行任何投影,则将无法在加载程序中使用生成的光标

public class ContactsWithCategorisProvider extends ContentProvider {

  private MySQLiteHelper database;

  // used for the UriMacher
  private static String TABLE_NAME = buildTableName();
  private static String TABLE_ID = ContactsTable._ID;
  private static String AUTHORITY = "some_authority";

  private static final int ITEMS = 10;
  private static final int ITEM_ID = 20;
  private static final String BASE_PATH = "yourbase";
  private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

  private static String buildTableName() {
    StringBuilder sb = new StringBuilder();
    sb.append(ContactsTable.TABLE_NAME);
    sb.append(" LEFT OUTER JOIN ");
    sb.append(ContactsCategoriesTable.TABLE_NAME);
    sb.append(" ON(");
    sb.append(ContactsCategoriesTable.CONTACT_ID);
    sb.append(" = ");
    sb.append(ContactsTable.CONTACT_ID);
    sb.append(" AND ");
    sb.append(ContactsCategoriesTable.CURRENT_USER);
    sb.append(" = ");
    sb.append(ContactsTable.CURRENT_USER);
    sb.append(") ");
    return sb.toString();
  }

  static {
    sURIMatcher.addURI(AUTHORITY, BASE_PATH, ITEMS);
    sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", ITEM_ID);
  }

  @Override public boolean onCreate() {
    database = new MySQLiteHelper(getContext());
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
      String sortOrder) {

    // Uisng SQLiteQueryBuilder instead of query() method
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    // check if the caller has requested a column which does not exists
    //      checkColumns(projection);
    Log.e("check_uri", uri.toString());
    // Set the table
    queryBuilder.setTables(TABLE_NAME);
    if (projection == null || projection.length == 0) {
      projection = new String[] {
          ContactsTable.TABLE_NAME + "." + ContactsTable._ID + " AS " + ContactsTable._ID,
          ContactsTable.NAME, ContactsTable.NAME_SHORT, ContactsTable.NUMBER, ContactsTable.NOTE,
          ContactsTable.CATEGORIES, ContactsCategoriesTable.NAME, ContactsTable.CONTACT_ID
      };
    }
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
      case ITEMS:
        break;
      case ITEM_ID:
        queryBuilder.appendWhere(TABLE_ID + "=" + uri.getLastPathSegment());
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor =
        queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
  }

  @Override public String getType(Uri uri) {
    return null;
  }

  @Override public Uri insert(Uri uri, ContentValues values) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    long id;
    switch (uriType) {
      case ITEMS:
        id = sqlDB.insert(TABLE_NAME, null, values);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(BASE_PATH + "/" + id);
  }

  public void insert(Uri uri, ArrayList<ContentValues> values) {

    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    switch (uriType) {
      case ITEMS:
        for (ContentValues c : values) {
          sqlDB.insert(TABLE_NAME, null, c);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
  }

  @Override public int delete(Uri uri, String selection, String[] selectionArgs) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsDeleted = 0;
    switch (uriType) {
      case ITEMS:
        rowsDeleted = sqlDB.delete(TABLE_NAME, selection, selectionArgs);
        break;
      case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
          rowsDeleted = sqlDB.delete(TABLE_NAME, TABLE_ID + "=" + id, null);
        } else {
          rowsDeleted =
              sqlDB.delete(TABLE_NAME, TABLE_ID + "=" + id + " and " + selection, selectionArgs);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsDeleted;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = database.getWritableDatabase();
    int rowsUpdated = 0;
    switch (uriType) {
      case ITEMS:
        rowsUpdated = sqlDB.update(TABLE_NAME, values, selection, selectionArgs);
        break;
      case ITEM_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
          rowsUpdated = sqlDB.update(TABLE_NAME, values, TABLE_ID + "=" + id, null);
        } else {
          rowsUpdated = sqlDB.update(TABLE_NAME, values, TABLE_ID + "=" + id + " and " + selection,
              selectionArgs);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsUpdated;
  }
}
公共类ContactsWithCategorisProvider扩展了ContentProvider{
私有MySQLiteHelper数据库;
//用于UriMacher
私有静态字符串TABLE_NAME=buildTableName();
私有静态字符串表\u ID=ContactsTable.\u ID;
私有静态字符串AUTHORITY=“some_AUTHORITY”;
专用静态最终整数项=10;
私有静态最终整数项_ID=20;
私有静态最终字符串BASE_PATH=“yourbase”;
私有静态最终UriMatcher sURIMatcher=新UriMatcher(UriMatcher.NO_匹配);
公共静态最终Uri CONTENT\u Uri=Uri.parse(“内容:/”+权限+“/”+基本路径);
私有静态字符串buildTableName(){
StringBuilder sb=新的StringBuilder();
sb.追加(ContactsTable.表格名称);
附加(“左外连接”);
sb.追加(ContactsCategoriesTable_NAME);
某人加上(“);
sb.追加(ContactsCategoriesTable.联系人ID);
某人加上(“=”);
sb.append(ContactsTable.CONTACT\u ID);
某人附加(“及”);
sb.追加(ContactsCategoriesTable.当前用户);
某人加上(“=”);
sb.追加(ContactsTable.当前用户);
某人加上(“)”;
使某人返回字符串();
}
静止的{
addURI(权限、基本路径、项目);
addURI(权限,基本路径+“/”,项目ID);
}
@重写公共布尔onCreate(){
database=newmysqlitehelper(getContext());
返回true;
}
@凌驾
公共游标查询(Uri、字符串[]投影、字符串选择、字符串[]选择、字符串,
字符串排序器){
//使用SQLiteQueryBuilder代替query()方法
SQLiteQueryBuilder queryBuilder=newsqlitequerybuilder();
//检查调用方是否请求了不存在的列
//检查列(投影);
Log.e(“check_uri”,uri.toString());
//摆桌子
setTables(表名称);
if(projection==null | | projection.length==0){
投影=新字符串[]{
ContactsTable.TABLE_NAME+“+ContactsTable._ID+”作为“+ContactsTable._ID,
ContactsTable.NAME,ContactsTable.NAME\u SHORT,ContactsTable.NUMBER,ContactsTable.NOTE,
ContactsTable.CATEGORIES,ContactsCategoriesTable.NAME,ContactsTable.CONTACT\u ID
};
}
int-uriType=sURIMatcher.match(uri);
开关(URI型){
个案项目:
打破
案例项目编号:
appendWhere(TABLE_ID+“=”+uri.getLastPathSegment());
打破
违约:
抛出新的IllegalArgumentException(“未知URI:+URI”);
}
SQLiteDatabase db=database.getWritableDatabase();
光标=
queryBuilder.query(数据库、投影、选择、选择、空、空、排序器);
cursor.setNotificationUri(getContext().getContentResolver(),uri);
返回光标;
}
@重写公共字符串getType(Uri){
返回null;
}
@重写公共Uri插入(Uri、ContentValues){
int-uriType=sURIMatcher.match(uri);
SQLiteDatabase sqlDB=database.getWritableDatabase();
长id;
开关(URI型){
个案项目:
id=sqlDB.insert(表名称,null,值);
打破
违约:
抛出新的IllegalArgumentException(“未知URI:+URI”);
}
getContext().getContentResolver().notifyChange(uri,null);
返回Uri.parse(基本路径+“/”+id);
}
公共void插入(Uri、ArrayList值){
int-uriType=sURIMatcher.match(uri);
SQLiteDatabase sqlDB=database.getWritableDatabase();
开关(URI型){
个案项目:
for(ContentValues c:values){
insert(表名称,null,c);
}
打破
违约:
抛出新的IllegalArgumentException(“未知URI:+URI”);
}
getContext().getContentResolver().notifyChange(uri,null);
}
@重写公共int delete(Uri、字符串选择、字符串[]selectionArgs){
int-uriType=sURIMatcher.match(uri);
SQLiteDatabase sqlDB=database.getWritableDatabase();
int rowsDeleted=0;
开关(URI型){
个案项目:
rowsDeleted=sqlDB.delete(表名称、所选内容、所选内容);
打破
案例项目编号:
String id=uri.getLastPathSegment();
if(TextUtils.isEmpty(选择)){
rowsDeleted=sqlDB.delete(表名称,表ID+“=”+ID,null);
}否则{
划船=
sqlDB.delete(表名,表ID+“=”+ID+”和“+selection,selectionArgs);
}
打破
违约:
抛出新的IllegalArgumentException(“未知URI:+URI”);
}
getContext().getContentResolver().notifyChange(uri,null);
返回删除的行;
}
@凌驾
公共int更新(Uri、ContentValues值、字符串选择、字符串[]选择