Android SQLite Error "requesting column name with table name"
So I ran into this problem while creating a Cursor
that would be passed to a SimpleCursorAdapter
. Turns out that while it's OK to prefix your 'query' columns String[], the subsequent String[] from
argument that's passed to the SimpleCursorAdapter
constructor does not need to be prefixed in order for the Adapter to map your result set correctly.
I got the same issue few days back and solved the issue using an alternative method. Previously I used the following method to set the cursor to point the column.
String sqlStatement = "SELECT supplierid,suppliercode,suppliername FROM supplierinfo WHERE suppliername LIKE '%"+query+"%' "; cursor = db.rawQuery(sqlStatement,null); if (cursor != null && cursor.getCount() > 0){ if (cursor.moveToFirst()){ do { int supplierId = cursor.getInt( 0); String supplierCode = cursor.getString(cursor.getColumnIndex( "suppliercode" )) != null ? cursor.getString( cursor.getColumnIndex( "suppliercode" ) ) : ""; String supplierName = cursor.getString(cursor.getColumnIndex( "suppliername" )) != null ? cursor.getString( cursor.getColumnIndex( "suppliername" ) ) : ""; supplierInfo = new SupplierInfo(); supplierInfo.setSupplierID( supplierId ); supplierInfo.setSupplierCode( supplierCode ); supplierInfo.setSupplierName( supplierName ); supplierInfoList.add(supplierInfo); } while (cursor.moveToNext()); }
After I changed this into the following method it worked for me.
String sqlStatement = "SELECT supplierid,suppliercode,suppliername FROM supplierinfo WHERE suppliername LIKE '%"+query+"%' "; cursor = db.rawQuery(sqlStatement,null); if (cursor != null && cursor.getCount() > 0){ if (cursor.moveToFirst()){ do { int supplierId = cursor.getInt( 0); String supplierCode = cursor.getString(1) != null ? cursor.getString( 1 ) : ""; String supplierName = cursor.getString(2) != null ? cursor.getString( 2 ) : ""; supplierInfo = new SupplierInfo(); supplierInfo.setSupplierID( supplierId ); supplierInfo.setSupplierCode( supplierCode ); supplierInfo.setSupplierName( supplierName ); supplierInfoList.add(supplierInfo); } while (cursor.moveToNext()); }