tjb
tjb

Reputation: 11728

Android SQLite Error "requesting column name with table name"

After running an sql query of the form:

SELECT table_name.column_name FROM table_name,table_name2,etc... WHERE condition1,condition2,etc...,

I get the following error, which does not shut down my program:

requesting column name with table name -- table_name.column_name

A google search for this error phrase led me to android.database.sqlite.SQLiteCursor line 314

A few lines above line 314 there is a comment that this code is a response to bug 903852. But I can't seem to find this bug on google.

So this is a two part question:

  1. Is it wrong to name the column name with the table in SQL? (I was under the impression that this was a best practice)
  2. How do I find Android bug report 903852 so that I can understand what the issue is? (googling Android bug 903852 doesn't work)

Upvotes: 15

Views: 8417

Answers (4)

Nuwan Wicks
Nuwan Wicks

Reputation: 31

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());
            }

Upvotes: 3

Animesh
Animesh

Reputation: 1757

In my case, the problem was solved when I used

select table_name.column_name as column_name_alt WHERE ....

and later, in my CursorAdapter, referred to it in the string array only as column_name_alt.

Hope this helps.

Upvotes: 11

thom_nic
thom_nic

Reputation: 8163

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.

Upvotes: 7

gssi
gssi

Reputation: 5087

I have found that the best practice is to surround all table names and condition values with single quotes! [I was getting 'unknown column name' errors in android even when the query worked in my stand-alone sqlite manager.]

Upvotes: 0

Related Questions