查询ContentProvider时出现“无效表”错误

时间:2013-01-11 05:49:02

标签: java android sqlite android-contentprovider

其次我是android的新手,并没有完全掌握它的工作方式(对于编写大部分教程的人来说似乎就是这种情况,我可以在网上找到,所以一个好的快速和脏的教程会也很棒)。 继续问题我会尝试只发布相关代码,如果我跳过某些内容请告诉我。

清单输入:

<provider android:name="MyContentProvider" android:authorities="hua.it20910.android.provider"/>

主要活动电话:

Cursor c = getContentProvider().query(MyContentProvider.getTableUri(1), null, null, null, null);

内容提供商:

private static SQLiteDatabase db;
private static final String database_name = "contactlist.db";
private static final int database_version = 1;
private static final String table1="Contacts";
private static final String[] table1_row={"_id","Surname","Name","Phone","Email","Address","Group"};
private static final String table2="Groups";
private static final String[] table2_row={"_id","Name"};

public static final String PROVIDER_NAME = "hua.it20910.android.provider";
public static final Uri CONTACTS_URI = Uri.parse("content://"+ PROVIDER_NAME + "/"+table1);
public static final Uri GROUPS_URI = Uri.parse("Content://"+PROVIDER_NAME + "/"+table2);

private static final int CONTACTS = 1;
private static final int CONTACT_ID = 2;
private static final int GROUPS = 3;
private static final int GROUP_ID = 4;

private static final UriMatcher myMatcher;
static {
    myMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    myMatcher.addURI(PROVIDER_NAME, table1, CONTACTS);
    myMatcher.addURI(PROVIDER_NAME, table1+"/#", CONTACT_ID);
    myMatcher.addURI(PROVIDER_NAME, table2, GROUPS);
    myMatcher.addURI(PROVIDER_NAME, table1+"/#", GROUP_ID);
}
private static final String table1Sql = "CREATE TABLE "
            + table1 + "(" 
            + table1_row[0] + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
            + table1_row[1] + " VARCHAR(25), "
            + table1_row[2] + " VARCHAR(25), " 
            + table1_row[3] + " INTEGER, " 
            + table1_row[4] + " VARCHAR(25), "
            + table1_row[5] + " VARCHAR(25)) ";

    private static final String table2Sql = "CREATE TABLE " 
            + table2 + "("
            + table2_row[0] + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + table2_row[1] + " VARCHAR(25))";


private class DatabaseHelper extends SQLiteOpenHelper{

    public DatabaseHelper(Context context) {
        super(context, database_name, null, database_version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(table1Sql);
        db.execSQL(table2Sql);
    }

    @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL("DROP TABLE IF EXISTS "+table1 );
         db.execSQL("DROP TABLE IF EXISTS "+table2 );
         onCreate(db);
      }     
}


public static Uri getTableUri(int i){
    if (i==1){
        return CONTACTS_URI;
    }
    if (i==2){
        return GROUPS_URI;
    }
    else{
        return null;
    }
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
    switch (myMatcher.match(uri)){
        case CONTACTS:
            sqlBuilder.setTables(table1);
            break;
        case CONTACT_ID:
            sqlBuilder.setTables(table1);
            sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
            break;
        case GROUPS:
            sqlBuilder.setTables(table2);
            break;
        case GROUP_ID:
            sqlBuilder.setTables(table2);
            sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
            break;
        default:
            throw new SQLException("Wrong uri " + uri);
    }
    if (sortOrder == null || sortOrder == "")
        sortOrder = "_id DESC";
    Cursor c = db.query(null, projection, selection, selectionArgs, null, null, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return null;
}

正如标题中所提到的,我当前得到的错误消息是执行查询时的无效表,但是我已经经历了一个工作周围的其他错误来到这里,我不确定我是否解决了它们刚刚走过它们只是为了在将来再次找到它们,所以任何纠错都会非常感激。

logcat的:

01-11 05:43:08.385: E/Trace(930): error opening trace file: No such file or directory (2)
01-11 05:43:08.525: I/ActivityThread(930): Pub hua.it20910.android.provider: hua.it20910.android.MyContentProvider
01-11 05:43:09.515: D/dalvikvm(930): GC_FOR_ALLOC freed 57K, 7% free 2583K/2760K, paused 191ms, total 193ms
01-11 05:43:09.515: I/dalvikvm-heap(930): Grow heap (frag case) to 3.241MB for 635812-byte allocation
01-11 05:43:09.565: D/dalvikvm(930): GC_FOR_ALLOC freed 1K, 6% free 3202K/3384K, paused 43ms, total 43ms
01-11 05:43:09.616: D/dalvikvm(930): GC_CONCURRENT freed <1K, 6% free 3214K/3384K, paused 5ms+13ms, total 52ms
01-11 05:43:09.635: D/AndroidRuntime(930): Shutting down VM
01-11 05:43:09.645: W/dalvikvm(930): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-11 05:43:09.655: E/AndroidRuntime(930): FATAL EXCEPTION: main
01-11 05:43:09.655: E/AndroidRuntime(930): java.lang.RuntimeException: Unable to start activity ComponentInfo{hua.it20910.android/hua.it20910.android.MainActivity}: java.lang.IllegalStateException: Invalid tables
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.os.Looper.loop(Looper.java:137)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 05:43:09.655: E/AndroidRuntime(930):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 05:43:09.655: E/AndroidRuntime(930):  at java.lang.reflect.Method.invoke(Method.java:511)
01-11 05:43:09.655: E/AndroidRuntime(930):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 05:43:09.655: E/AndroidRuntime(930):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 05:43:09.655: E/AndroidRuntime(930):  at dalvik.system.NativeStart.main(Native Method)
01-11 05:43:09.655: E/AndroidRuntime(930): Caused by: java.lang.IllegalStateException: Invalid tables
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.database.sqlite.SQLiteDatabase.findEditTable(SQLiteDatabase.java:971)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
01-11 05:43:09.655: E/AndroidRuntime(930):  at hua.it20910.android.MyContentProvider.query(MyContentProvider.java:178)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.content.ContentProvider.query(ContentProvider.java:652)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.content.ContentResolver.query(ContentResolver.java:372)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.content.ContentResolver.query(ContentResolver.java:315)
01-11 05:43:09.655: E/AndroidRuntime(930):  at hua.it20910.android.MainActivity.displayRecords(MainActivity.java:31)
01-11 05:43:09.655: E/AndroidRuntime(930):  at hua.it20910.android.MainActivity.onCreate(MainActivity.java:19)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.Activity.performCreate(Activity.java:5104)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-11 05:43:09.655: E/AndroidRuntime(930):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-11 05:43:09.655: E/AndroidRuntime(930):  ... 11 more

1 个答案:

答案 0 :(得分:1)

  

正如标题中所提到的,我目前得到的错误消息是   执行查询时无效的表,但我经历了一个地狱   工作周围的其他错误来到这里,我不知道我是否   解决了他们[...]

当您在null的{​​{1}}方法中执行sqlite查询时,您通过query作为表名,这是正常的,这一行:

ContentProvider

第一个参数是表名,您必须提供有效的表名才能使查询成功。因此,基于Cursor c = db.query(null, projection, selection, selectionArgs, null, null, sortOrder); ,您可以选择正确的表并指定有效的表名。另外,如果您要完全忽略它并简单地直接查询数据库,那么在同一查询方法中使用Uri的目的是什么?

关于教程,我认为你错过了最重要的一个,the official guide for ContentProviders,你甚至有一个在那里建立一个的例子。