r0t0r r0t0r - 2 years ago 142
Java Question

"Invalid Tables" error when querying a ContentProvider

Secondly I'm really new in android and haven't completely grasped the way it works (which seems to be the case for the people who wrote most of the tutorials I could find online sadly so a good quick and dirty tutorial would also be great).
Moving on with the problem I'll try to post only relevant code, if I skip something please let me know.

Manifest Entry:

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

Main activity call:

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

Content Provider:

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[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[1] + " VARCHAR(25))";

private class DatabaseHelper extends SQLiteOpenHelper{

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

public void onCreate(SQLiteDatabase db) {

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

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

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
switch (myMatcher.match(uri)){
sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
case GROUPS:
case GROUP_ID:
sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
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;

As mentioned in the title the error message I'm currently getting is Invalid tables when executing the query but I've been through a hell of working arround other errors to get here and I'm not sure if I solved them or just walked arround them just to find them again in the future so any error correcting would be greatly appreciated.


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

Answer Source

As mentioned in the title the error message I'm currently getting is Invalid tables when executing the query but I've been through a hell of working arround other errors to get here and I'm not sure if I solved them[...]

It's normal that you get that exception because you pass null as the table name when you do the sqlite query in the query method of the ContentProvider, this line:

Cursor c = db.query(null, projection, selection, selectionArgs, null, null, sortOrder);

The first parameter is the table name and you must supply a valid table name for the query to be successful. So based on the Uri you get choose the proper table and assign a valid table name. Also, what is the purpose of using a SQLiteQueryBuilder in the same query method if you're going to ignore it completely and simply query the database directly?

Regarding tutorials, I think you missed the most important one, the official guide for ContentProviders, you even have an example of building one there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download