dimanche 15 février 2015

Android sqLite database Column not found in some code, but found in other code

I'm looking to update a particular column in a row (for the first time). But when my method checks if the column exists it returns false. But when I do a query of all columns to an array, the column name is found in the array (both approaches were used as a test in the code below). As a side not, the line



return database.update(myTable, values, IdColumn + "=" + myId, null) > 0;


also returns false, at this point I'm guessing it's because it can't find the column. Than you for the help. Here's the code snippet:


Edit: I'm not sure this matters, but no values had yet been put into any of the rows for the column in question.



public boolean set_Column_StrVal (Context context, dbData dbData, String myColumn, String myString) {

String myTable = dbData.getTable();
long myId = dbData.getId();
boolean bool = doesColumnExist(myTable, myColumn);

Cursor cursor = database.query(myTable,null,null,null,null,null,null);
String[] columnNames = cursor.getColumnNames();
System.out.println(Arrays.toString(columnNames));
boolean boolArray = Arrays.asList(columnNames).contains(myColumn);

Toast toast = Toast.makeText(context, "myTable = " + myTable + " myColumn = " +
myColumn + " myString = " + myString + " bool = " + String.valueOf(bool) + " myId = " +
String.valueOf(myId) + " boolArray = " + boolArray, Toast.LENGTH_SHORT);
toast.show();

if (!bool)
{
return false;
}

ContentValues values = new ContentValues();
values.put(myColumn, myString);
return database.update(myTable, values, IdColumn + "=" + myId, null) > 0;
}


private boolean doesColumnExist(String myTable, String myColumn) {
boolean doesExist = true;

Cursor cursor = database.rawQuery("PRAGMA table_info(" + myTable + ")",null);
int value = cursor.getColumnIndex(myColumn);

if(value == -1)
{
doesExist = false;
}
return doesExist;


}


Aucun commentaire:

Enregistrer un commentaire