Reputation: 4236
Let me start of by explaining how I insert and return data from SQLite.
First I create the table like this:
private static final String CREATE_TABLE_STUDENT_LESSON = " create table STUDENTSPECIFICLESSON ( _id TEXT , _viewID INTEGER PRIMARY KEY AUTOINCREMENT , _LESSON_TITLE TEXT , _LESSON_DATE TEXT , _LESSON_PROBLEM TEXT );";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENT_LESSON);
}
Then I insert into the table like this in SQLite:
public void insertLesson(String _id, String lessonTitle, String lessonDate, String lessonProblem) {
ContentValues contentValue = new ContentValues();
contentValue.put(SQLiteHelper._ID, _id);
contentValue.put(SQLiteHelper.LESSON_TITLE, lessonTitle);
contentValue.put(SQLiteHelper.LESSON_DATE, lessonDate);
contentValue.put(SQLiteHelper.LESSON_PROBLEM, lessonProblem);
this.getWritableDatabase().insert(SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON, null, contentValue);
}
And I insert it from the Activity like this:
sqLiteHelper.insertLesson(id,etLessonTitle.getText().toString(),currentDateandTime,etLessonProbStu.getText().toString());
You will notice that I have created _viewID INTEGER PRIMARY KEY AUTOINCREMENT
I'm using this to get the specific ViewHolder in my Adapter.
I get this _viewID
by doing this:
public String getLessonViewHolderID(String _path){
String id = null;
Cursor cursor = getReadableDatabase().rawQuery("Select "+SQLiteHelper._viewID+" from "+SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON+" Where "
+SQLiteHelper.LESSON_TITLE +"='"+_path+"'",null);
if (cursor != null) {
cursor.moveToFirst();
}
if (cursor == null) {
} else if (cursor.moveToFirst()) {
do {
id = String.valueOf(cursor.getInt(cursor.getColumnIndex(SQLiteHelper._viewID)));
} while (cursor.moveToNext());
cursor.close();
} else {
}
return id;
}
This works perfectly and returns the _viewId
correctly as expected.
I then try to get _LESSON_PROBLEM
from SQLite by doing this:
public String getLessonProblem(String _id){
String id = null;
Cursor cursor = getReadableDatabase().rawQuery("Select _LESSON_PROBLEM from "+SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON+" Where "
+SQLiteHelper._viewID +"='"+_id+"'",null);
if (cursor != null) {
cursor.moveToFirst();
}
if (cursor == null) {
} else if (cursor.moveToFirst()) {
do {
id = String.valueOf(cursor.getInt(cursor.getColumnIndex("_LESSON_PROBLEM")));
} while (cursor.moveToNext());
cursor.close();
} else {
}
return id;
}
and from my Adapter
I call:
String lessonProblem = helper.getLessonProblem(viewId);
The above String lessonProblem
will then return 0
.
I have checked my database and all the data is inserted correctly, here is a image of my database:
I can't understand why it is returning 0 when clearly it is not, can someone please help me point out what the issue may be?
Upvotes: 3
Views: 1440
Reputation: 75798
Sloppy mistake. You should use getString()
instead of getInt()
.
Returns the value of the requested column as a String. The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
Finally
cursor.getString(cursor.getColumnIndex("_LESSON_PROBLEM"));
Upvotes: 7
Reputation: 152927
id = String.valueOf(cursor.getInt(cursor.getColumnIndex("_LESSON_PROBLEM")));
You're getting a string value as an int and then converting that to a string. Change that to something like
id = cursor.getString(cursor.getColumnIndex("_LESSON_PROBLEM"));
Upvotes: 5