Android SQLite:从表中检索值时调用getColumnIndex是一种好的做法吗?

Android SQLite:从表中检索值时调用getColumnIndex是一种好的做法吗?,android,android-cursor,android-sqlite,Android,Android Cursor,Android Sqlite,现在,我的代码如下所示: SqlTables.java: // Separate file public enum SqlTables { TABLE_1("table_1"), . . . TABLE_N("table_n"); final String name; private SqlTables(String name) { this.name = name; } } // Separate file pu

现在,我的代码如下所示:

SqlTables.java:

// Separate file
public enum SqlTables {
    TABLE_1("table_1"),
    .
    .
    .
    TABLE_N("table_n");
    final String name;
    private SqlTables(String name) {
        this.name = name;
    }
}
// Separate file
public enum Table1Columns {
    ...
}
Cursor c = db.query(SqlTables.TABLE_1.toString(), null...);
final int indexId = c.getColumnIndexOrThrow(
        Table1Columns.ID.toString());
final int indexName = c.getColumnIndexOrThrow(
        Table1Columns.NAME.toString());
while (c.moveToNext()) {
    final String id = c.getString(indexId);
    final String name = c.getString(indexName);
}
Table1Columns.java:

// Separate file
public enum SqlTables {
    TABLE_1("table_1"),
    .
    .
    .
    TABLE_N("table_n");
    final String name;
    private SqlTables(String name) {
        this.name = name;
    }
}
// Separate file
public enum Table1Columns {
    ...
}
Cursor c = db.query(SqlTables.TABLE_1.toString(), null...);
final int indexId = c.getColumnIndexOrThrow(
        Table1Columns.ID.toString());
final int indexName = c.getColumnIndexOrThrow(
        Table1Columns.NAME.toString());
while (c.moveToNext()) {
    final String id = c.getString(indexId);
    final String name = c.getString(indexName);
}
SqlDatabaseInterface.java:

// Separate file
public enum SqlTables {
    TABLE_1("table_1"),
    .
    .
    .
    TABLE_N("table_n");
    final String name;
    private SqlTables(String name) {
        this.name = name;
    }
}
// Separate file
public enum Table1Columns {
    ...
}
Cursor c = db.query(SqlTables.TABLE_1.toString(), null...);
final int indexId = c.getColumnIndexOrThrow(
        Table1Columns.ID.toString());
final int indexName = c.getColumnIndexOrThrow(
        Table1Columns.NAME.toString());
while (c.moveToNext()) {
    final String id = c.getString(indexId);
    final String name = c.getString(indexName);
}

我没有找到任何关于Android如何将列映射到索引的文档,因此虽然我已经确认它是基于查询中列的顺序,但我不能保证这将在100%的时间内工作,或者未来的更新将保持这种行为。使用
getColumnIndexOrThrow(…)
的好处是,只要它们引用的列包含在查询中,就可以保证我的索引始终是正确的。缺点是,除了繁琐之外,它使我的代码更难阅读,并大大增加了我的方法的长度。另一方面,我可以只指定索引而不调用
getColumnsIndexOrThrow
,这会缩短我的代码,但是缺少文档会让我觉得这是错误的。

不,您绝对不应该将列索引值硬编码到代码中。如果这样做,数据库将几乎无法管理(因为更改表可能会导致索引值更改)

据我所知,您的代码很难阅读,因为:

  • 您正在使用对
    toString()
    的不必要调用来编写代码。只要把它们处理掉。。。它们是完全多余的

  • 您使用的
    final
    关键字太多,会使您的代码变得混乱。在可能意外更改变量值的情况下,应将此作为预防措施。这不是那种情况

  • 您正在跨多行传播简单语句。只要用一行就可以了,这样读起来就容易多了


  • 也就是说,上述任何编码实践在技术上都没有问题。我只是说,如果你真的想让你的代码易于阅读,那么你可能会把它们当作建议。使用
    getColumnIndex()
    getColumnIndexOrThrow()
    打字时间更长,但如果您在查询中重新排序或创建小的更改,则会更安全。
    如果您这样做,对数据库进行更改几乎不可能管理。
    --因为……因为如果他更改了数据库架构(即在他的一个表格中添加一列),无法保证整数值会保持不变。像这样一个微妙的错误可能会花费他数小时的调试时间。这就是为什么在程序执行期间,在运行时检查列索引的整数值是非常非常好的做法。哈哈,好吧……在有人指出我推理中的缺陷之前,我坚持这一点回答并保持100%的信心,相信这个答案是正确的。@RobertHarvey,顺便说一句,答案也同意我的观点。:)我只是指出你的答案没有完成P