2012-01-27 74 views
6

虽然与SQLiteCursor在Android的工作我才知道,在getColumnIndex()的行为区分大小写例如:Android - SQLite Cursor getColumnIndex()区分大小写?

例子:

Column Name in DB was: Rules 
cursor.getColumnIndex("Rules") //workes fine 
cursor.getColumnIndex("rules") //throws error, see the error detail 

的文档没有提到的是,对于细节please see this

logcat的说:

java.lang.IllegalStateException:无法从 CursorWindow逐行读取0,-1关口。确保光标之前我对SQLiteCursor的这种行为感到困惑从中

访问数据正确初始化,有人可以帮助我,这是真的还是我做错了什么?如果需要,我可以提供代码。

谢谢。

+1

那么你的问题是什么? – 2012-01-27 06:31:27

+0

请现在看看我的问题。 – 2012-01-27 06:33:34

回答

3

getColumnIndex()是大小写敏感的:在DB

列名是:规则

cursor.getColumnIndex( “规则”)// workes精细

光标。getColumnIndex(“规则”)//抛出错误,请参阅错误详细

+4

我无法想象为什么在地球上他们不正常化列名称,所以他们将不区分大小写。 SQL本身并不关心这种情况,为什么要使用Cursor? – nobre 2012-07-12 15:12:30

+0

有关'为什么'的一些信息:https://code.google.com/p/android/issues/detail?id=42636 – 2014-03-28 09:57:38

1

使用SQLite最好的和推荐的方法是,你宣布你的所有的表名和列名staticfinalclass水平。例如:

// 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); 
使用

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

+0

非常感谢。但实际上我不是使用代码创建我的数据库。我需要使用现有的数据库。为什么我需要它?因为它有一些默认数据。我尝试了“如何为数据库提供一些默认数据”的不同方法,并且我发现它是使用默认数据创建和填充数据库的最佳方式。 – 2012-01-27 06:57:48

+0

那么你需要探索数据库,看到表和列的名称一次,然后使用它们的变量:) – 2012-01-27 07:01:36

1

另一种方式是通过使用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");

是的,我知道这将是艰难的数据库。但它的工作原理和稳定