让我首先解释如何从SQLite插入和返回数据.
首先,我创建这样的表:
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);
}
Run Code Online (Sandbox Code Playgroud)
然后我在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);
}
Run Code Online (Sandbox Code Playgroud)
我从Activity中插入它,如下所示:
sqLiteHelper.insertLesson(id,etLessonTitle.getText().toString(),currentDateandTime,etLessonProbStu.getText().toString());
Run Code Online (Sandbox Code Playgroud)
您会注意到我创建了_viewID INTEGER PRIMARY KEY AUTOINCREMENT我正在使用它来获取我的适配器中的特定ViewHolder.
我_viewID通过这样做得到这个:
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;
}
Run Code Online (Sandbox Code Playgroud)
这完美_viewId地工作并按预期正确返回.
然后我尝试_LESSON_PROBLEM通过这样做来从SQLite:
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;
}
Run Code Online (Sandbox Code Playgroud)
从我的Adapter电话:
String lessonProblem = helper.getLessonProblem(viewId);
Run Code Online (Sandbox Code Playgroud)
以上String lessonProblem将返回0.
我检查了我的数据库并正确插入了所有数据,这是我的数据库的图像:
我不明白为什么它显然返回0时显然不是,有人可以帮我指出问题可能是什么?
马虎的错误.你应该使用getString()而不是getInt().
以String形式返回所请求列的值.结果以及当列值为null或列类型不是字符串类型时此方法是否抛出异常是实现定义的.
最后
cursor.getString(cursor.getColumnIndex("_LESSON_PROBLEM"));
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)id = String.valueOf(cursor.getInt(cursor.getColumnIndex("_LESSON_PROBLEM")));
您将字符串值作为int获取,然后将其转换为字符串.把它改成类似的东西
id = cursor.getString(cursor.getColumnIndex("_LESSON_PROBLEM"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
550 次 |
| 最近记录: |