10

在 Android 中使用 SQLiteCursor 时,我知道getColumnIndex()的行为区分大小写,例如:

例子:

DB中的列名是:规则

cursor.getColumnIndex("Rules")  //works fine
cursor.getColumnIndex("rules")  //throws error, see the error detail

文档对此只字未提,有关详细信息, 请参阅this

LogCat 说:

java.lang.IllegalStateException:无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标

我对这种行为感到困惑SQLiteCursor,有人可以帮我确定这是真的还是我做错了什么?如果需要,我可以提供代码。

谢谢。

4

4 回答 4

5

getColumnIndex() 区分大小写:

DB中的列名是:规则

cursor.getColumnIndex(" Rules ") //工作正常

cursor.getColumnIndex(" rules ") //抛出错误,查看错误详情

于 2012-03-07T15:11:47.430 回答
2

使用 SQLite 的最佳和推荐方法是声明所有表名和列名staticfinal以及class级别.. 例如:

// write table name
public static final String TABLE_MESSAGE = "messages";
// and column name accordingly
public static final String COLUMN_ID = "_id";
public static final String COLUMN_MESSAGE = "message";

所以这种方法的好处是你不需要记住表和列名的拼写和大小写等。

当您访问任何表或列时,您只需使用这些静态变量,例如:

// TABLE creation sql statement
private static final String TABLE_CREATE = "create table "
            + TABLE_MESSAGE + "( " + COLUMN_ID
            + " integer primary key autoincrement, " + COLUMN_MESSAGE
            + " text not null);";

查询时:

database.query(TABLE_MESSAGE, new String[]{COLUMN_ID,COLUMN_MESSAGE}, null, null, null, null, null);

或者它可以用于光标

int index = cursor.getColumnIndex(COLUMN_MESSAGE);

这将帮助您避免这种区分大小写和拼写错误的冲突。:)

于 2012-01-27T06:49:01.973 回答
2

另一种方法是使用 查询数据库本身以获取正确的名称PRAGMA table_info,因此我为此编写了一个方法:

public class database {
    private SQLiteDatabase mainDB = null;

    private boolean CreateOrOpenDB() {
        try {
            if (mainDB == null || !mainDB.isOpen()) {
                mainDB = Context.openOrCreateDatabase("mainDB", SQLiteDatabase.CREATE_IF_NECESSARY, null);
            }
        } catch (SQLiteException e) {
            return false;
        }
        return true;
    }

    private String GetTrueColumnName(String TableName, String column) {
        String TrueColName = "";
        if (CreateOrOpenDB()) {
            try {
                Cursor c = mainDB.rawQuery("PRAGMA table_info(" + TableName + ");", null);

                if (c != null) {
                    if (c.moveToFirst()) {
                        do {
                            String dbcolumn = c.getString(c.getColumnIndex("name"));
                            if (column.toLowerCase().equals(dbcolumn.toLowerCase())) {
                                TrueColName = dbcolumn;
                                break;
                            }
                        } while (c.moveToNext());
                    }
                    c.close();
                }
                mainDB.close();
            } catch (Exception e) {
            }
        }
        return TrueColName;
    }
}

那么您需要调用的是:

String CorrectName = GetTrueColumnName(TableName, "RuLeS");

是的,我知道这对数据库来说会很困难。但它有效且稳定

于 2015-04-04T17:02:10.477 回答
1
return readableDatabase
            .query(
                ProductosContract.ProductosEntry.TABLE_NAME,
                ProductosContract.ProductosEntry.ALL_COLUMNS_NAME_ALIAS, null, null, null, null, null
            )

您可以指定要检索的列,在该参数中将列名别名添加为小写,例如 (Kotlin):

arrayOf("name as 'name'")

所以你总是会得到小写字母。使用小写字母或您喜欢的字母,它会起作用。

于 2019-09-04T21:11:48.073 回答