lundi 16 février 2015

Android SQLite cursor getType appears to be crashing app

My app is crashing during getType. The method below is supposed to cycle through all of the columns of the Table in a database and fill with a simple value.


Why would I do that?


Basically, I generally insert a row with a value in only one column. I then want to come back later and insert values in the other column, but when I try to update a column in a specific row for the column that was not given a value during the row's insert, the app crashes, or when I use getColumnIndex in the table with no values yet applied in the column in question, the app crashes. (For some reason, the getColumnNames will return the column name within the array, even if no rows have a value for the column.)


This comes down to my theory that if there is no value in a column, it will crash when using certain methods, even though I cannot seem to find anything to support this theory. But I'm trying to test this theory by applying a value to all columns by way of...



ContentValues values = setContVal_All_Columns(myTable); //myTable being the table name
long insertId = database.insert(myTable, null, values); //database being my database


...using my method below. But as I said above, this method is crashing at getType() So I have two questions:


1)What can make getType crash the app


2)Should I be able to read and write to a column that was not given a value upon insert?


3)my goal is to be able to insert a row with values in one or two columns, then update columns later. Are there examples of this that address my issues above?


Thank you!



//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();

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;
}

Aucun commentaire:

Enregistrer un commentaire