Android SQLite 错误 "requesting column name with table name"

15

运行类似下面的 SQL 查询:

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

我遇到了以下错误,但它并没有关闭我的程序:

请求带有表名的列名 -- table_name.column_name

在谷歌上搜索这个错误短语,我找到了android.database.sqlite.SQLiteCursor第314行

在第314行之前的几行中,有一个注释说,这段代码是对错误903852的响应。但我似乎在谷歌上找不到这个错误。

所以这是一个两部分的问题:

  1. 在SQL中使用表名给列命名是否有错?(我原本认为这是最佳实践)
  2. 如何找到Android bug报告903852,以便我可以了解问题是什么?(搜索“Android bug 903852”不起作用)

你能发一下你的代码吗? - Chirag
只是为了让你开始运行:从表名a中选择列名a.column_name,其中...这并不能回答你的问题,但如果你卡住了,这应该会有所帮助。 - Harald Wilhelm
请参阅AbstructCursor does not allow to use column name with period了解有关问题#2的详细信息。 - ooi
4个回答

11

在我的情况下,当我使用了某个方法时,问题得到了解决。

select table_name.column_name as column_name_alt WHERE ....

后来,在我的CursorAdapter中,我仅在字符串数组中将其称为column_name_alt

希望能对你有所帮助。


1
将该问题的良好描述和与此回复中类似的建议翻译为中文:http://massimilianosciacco.com/sqlite-cursor-and-table-alias-bug-android-database - shtolik

7

我在创建一个传递给SimpleCursorAdapterCursor时遇到了问题。结果发现,虽然在您的“query”列String[]中添加前缀是可以的,但是传递给SimpleCursorAdapter构造函数的后续String[] from参数不需要添加前缀,这样适配器才能正确地映射您的结果集。


3

我几天前遇到了同样的问题,并使用另一种方法解决了该问题。之前,我使用以下方法来设置光标指向列。

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

当我将代码更改为以下方法后,它对我起作用了。

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

对于那些不想逐字逐句比较两个代码块的人来说,区别似乎在于将列索引参数硬编码到 getString() 中,而不是使用 getColumnIndex()。这听起来并不像是解决所述问题的方法。 - LarsH

0

我发现最佳实践是将所有表名和条件值都用单引号括起来![即使查询在我的独立sqlite管理器中工作,但在Android中仍然会出现“未知列名”错误。]


这个回答似乎与问题无关。OP并没有谈论“未知列名”错误。 - LarsH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接