Android SQlite returning -1 with getColumnIndex when column obviously exists

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

When inserting temporary values to all of the columns in my table, it appears to work. See method below and log below. But when I test if the column exists with my test method below, it returns a false that the column exists!!

The first snippet of code shows from where the temporary values and the method for testing if a column exists is called. The snippets below include the log, how the table is created, and the called methods. Thank you!

ContentValues values = setContVal_All_Columns(myTable);  //create row long insertId = database.insert(myTable, null, values); System.out.println("Column date exists = " + String.valueOf(doesColumnExist(myTable,"date")));

setContVal_All_Columns Method:

//sets generic content values to initialize row = excluding ID column private ContentValues setContVal_All_Columns(String myTable) {      ContentValues contentValues = new ContentValues();     Cursor cursor = database.query(myTable,null,null,null,null,null,null);     String[] columnNames = cursor.getColumnNames();     cursor.moveToFirst();      for(String name : columnNames) {         if(!name.equals(IdColumn)) {  //excludes Id column             int index = cursor.getColumnIndex(name);             System.out.println("Column name = " + name + " index = " + String.valueOf(index));             System.out.println("Type = " + String.valueOf(cursor.getType(index))); //appears to crash on the getType             if (cursor.getType(index) == 3) {  //String                 System.out.println("Column is string");                 contentValues.put(name, " ");             } else if (cursor.getType(index) == 1) {  //integer                 contentValues.put(name, 0);             }         }     }      return contentValues; }

METHOD FOR TESTING IF COLUMN EXISTS: always returning -1 on date column...

private boolean doesColumnExist(String myTable, String myColumn) {     boolean doesExist = true;  Cursor cursor = database.rawQuery("PRAGMA table_info(" + myTable + ")",null); cursor.moveToFirst();  int value = cursor.getColumnIndex(myColumn);  if(value == -1) {     doesExist = false; } return doesExist; }

LOG INFO:

02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = date index = 1 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 0 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = name index = 2 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 3 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column is string 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = altitude index = 3 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 0 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = is_used index = 4 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 0 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = warning index = 5 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 0 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Column name = action index = 6 02-16 19:58:42.296    9711-9711/com.mycompany.dudesmyreminders I/System.out Type = 0 02-16 19:58:42.316    9711-9711/com.mycompany.dudesmyreminders I/System.out Column date exists = false

Table Creation Method:

//Database creation sql statement private static final String SQL_CREATE_SPECIAL_DAYS =         "CREATE TABLE " + dbFields.TABLE_NAME_SPECIAL_DAYS + " (" +                 dbFields.COLUMN_SPECIAL_DAYS_ID + INTEGER_PRIMARY_KEY + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_DATE + TEXT_TYPE + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_NAME + TEXT_TYPE + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_ALTITUDE + INTEGER_TYPE + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_USED + INTEGER_TYPE + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_WARNING + INTEGER_TYPE + COMMA_SEP +                 dbFields.COLUMN_SPECIAL_DAYS_ACTION + INTEGER_TYPE +                 // Any other options for the CREATE command                 " )";    @Override public void onCreate(SQLiteDatabase database) {     database.execSQL(SQL_CREATE_SPECIAL_DAYS); }  @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {     Log.w(MySQLiteHelper.class.getName(),             "Upgrading database from version " + oldVersion + " to "                     + newVersion + ", which will destroy all old data");     db.execSQL("DROP TABLE IF EXISTS " + dbFields.TABLE_NAME_SPECIAL_DAYS);     onCreate(db); }

回答1:

With

cursor.moveToFirst(); int value = cursor.getColumnIndex(myColumn);

You're assuming that PRAGMA would return all the columns in a single row. That's now how it works. From the PRAGMA documentation

PRAGMA table_info returns one row for each column in the named table. Columns in the result set include the column name, data type, whether or not the column can be NULL, and the default value for the column.

So, your doesColumnExist() method should look something like

boolean doesExist = false; Cursor cursor = database.rawQuery("PRAGMA table_info(" + myTable + ")",null);  while (cursor.moveToNext()) {     if (cursor.getString(cursor.getColumnIndex("name")).equals(myColumn)) {         doesExist = true;         break;     } }  cursor.close(); return doesExist;

Also, do not forget to close your Cursors.



回答2:

From SQLite documentation:

PRAGMA table_info(table-name);
This pragma returns one row for each column in the named table.

So the result of your PRAGMA query looks roughly like this:

|------------------------------------------------------- |cid | name     | type    | not null | dflt_value | pk |   // <-- you're trying to find your column names here, but instead they are in each of the rows |------------------------------------------------------- | 1  | date     | text    | 1        |            | 0  | |------------------------------------------------------- | 2  | name     | text    | 0        |            | 0  |  |------------------------------------------------------- | 3  | altitude | integer | 1        |            | 0  | |-------------------------------------------------------   .......                                               |-------------------------------------------------------


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!