IllegalArgumentException: invalid column

Here is logcat:

01-15 16:06:03.622: ERROR/AndroidRuntime(22300): Uncaught handler: thread main exiting due to uncaught exception 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mohit.geo2do/com.mohit.geo2do.activities.TaskEdit}: java.lang.IllegalArgumentException: Invalid column due_date 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.os.Handler.dispatchMessage(Handler.java:99) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.os.Looper.loop(Looper.java:123) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread.main(ActivityThread.java:4363) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at java.lang.reflect.Method.invokeNative(Native Method) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at java.lang.reflect.Method.invoke(Method.java:521) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at dalvik.system.NativeStart.main(Native Method) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): Caused by: java.lang.IllegalArgumentException: Invalid column due_date 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:508) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:356) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:309) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:266) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at com.mohit.geo2do.provider.TasksProvider.query(TasksProvider.java:174) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.content.ContentProvider$Transport.query(ContentProvider.java:130) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.content.ContentResolver.query(ContentResolver.java:202) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at com.mohit.geo2do.activities.TaskEdit.onCreate(TaskEdit.java:105) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-15 16:06:03.657: ERROR/AndroidRuntime(22300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 

The line associated with it:

 private Cursor task; private Uri uri; private String[] PROJECTION { Tasks._ID, Tasks.TITLE, Tasks.COMPLETED, Tasks.DUE_DATE, Tasks.IMPORTANCE, Tasks.NOTES }; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_task); ... uri = getIntent().getData(); task = getContentResolver().query(uri, PROJECTION, null, null, null); } ... 

What could be the problem? The database was created just fine. Is there any other code that you will need to see?

UPDATE:
I am VERY sure this column exists. I requested the database as follows:

 Cursor c = db.rawQuery("SELECT * FROM tasks LIMIT 1", null); for (int i = 0; i < c.getColumnNames().length; i++) { Log.v(TAG, c.getColumnNames()[i]); } 

And in LogCat:

 01-15 16:52:07.857: VERBOSE/TasksProvider(24325): Creating database... 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): _id 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): title 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): completed 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): due_date 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): notes 01-15 16:52:07.862: VERBOSE/TasksProvider(24325): importance 

So the column exists.

+8
java android android-contentprovider sqlite illegalargumentexception
source share
3 answers

I found a weird solution. In String[] PROJECTION . You must do:

 private String[] PROJECTION { Tasks._ID, Tasks.TITLE, Tasks.COMPLETED, Tasks.DUE_DATE + " as " + Tasks.DUE_DATE, Tasks.IMPORTANCE + " as " + Tasks.DUE_DATE, Tasks.NOTES + " as " + Tasks.NOTES }; 
+7
source share

A column certainly exists in your database, but if you did not add a column to an object called a projection map, you will get the "invalid column" error that you see. You can add a projection map through the query builder object, for example:

 // The projection map is a hashmap of strings HashMap<String, String> MyProjectionMap; MyProjectionMap = new HashMap<String, String>(); // Add column mappings to the projection map MyProjectionMap.put(Tasks._ID, Tasks._ID); MyProjectionMap.put(Tasks.TITLE, Tasks.TITLE); [...] SQLiteQueryBuilder qb; qb.setTables("tasks"); qb.setProjectionMap(MyProjectionMap) // Then do your query Cursor c = qb.query(db, projection, ...) 

To understand what is going on, look in the source for the SQLiteQueryBuilder class and you will see the following:

 private String[] computeProjection(String[] projectionIn) { if (projectionIn != null && projectionIn.length > 0) { if (mProjectionMap != null) { [...] for (int i = 0; i < length; i++) { String userColumn = projectionIn[i]; String column = mProjectionMap.get(userColumn); [...] if (!mStrictProjectionMap && ( userColumn.contains(" AS ") || userColumn.contains(" as "))) { /* A column alias already exist */ projection[i] = userColumn; continue; } throw new IllegalArgumentException("Invalid column " + projectionIn[i]); } } [...] 

It basically checks the columns that you requested in your projection for a list of "allowed" columns, and you can see that if the map does not contain a column from your projection, it will throw an IllegalArgumentException, as you saw. (I believe this check against the map is a security feature that prevents SQL-based attacks from people abusing your content provider, but this is just an assumption.)

Please also note that if you install "strict" projection maps in the query builder:

 qb.setStrictProjectionMap(true); 

Then in this case, he expects you to know the exact column names ... If you do not set it, he checks the alias of the "AS" column - I think this explains the "strange fix" that you found.

Hope this helps.

+17
source share

Have you added this column to the content provider forecast map relative to the table with this column? Hope this helps.

0
source share

Source: https://habr.com/ru/post/651104/


All Articles