jason
jason

Reputation: 427

Simple insert statement not working. SqLite

I seem to have run into difficulties with a simple database insert statement. I'm inserting something into the database then (for debugging reasons) checking the number of rows. For the first example this doesn't work. For the second "rawQuery" it does. Am I overlooking something simple? I've used this method loads of times before with no problems!

This doesn't work:

contentValues = new ContentValues();
contentValues.put("classId", cursor.getString(0));
contentValues.put("to_do", toDo);
contentValues.put("date", date);
out("SIZE OF TODO+"+fetchCount());//returns 105
db.insert("toDo", null, contentValues);
out("NEW SIZE OF TODO+"+fetchCount());//still returns 105!

this does:

out("SIZE OF TODO+"+fetchCount()); //returns 105
db.execSQL("insert into toDo values(null, "+cursor.getString(0)+", '"+toDo+"', '"+date+"')");
out("NEW SIZE OF TODO+"+fetchCount());//returns 106

What's going wrong?

Upvotes: 0

Views: 2526

Answers (2)

jason
jason

Reputation: 427

I'm such an idiot, @Rob's suggestion of looking at my create tables made me realise, I was referring to the wrong column name. It never occurred to me that an update statement like this could fail silently! Thanks @Rob

Upvotes: 0

Rob Pridham
Rob Pridham

Reputation: 4928

At a guess: lines 3 and 4 are not inserting strings into the ContentValues set, but the raw objects instead. Later, some type conversion is failing. Call toString() on your parameters.

In the second excerpt, you are implicitly forcing conversion to string.

Upvotes: 1

Related Questions