我有一张桌子的question_table
和一张ImageButton
(背面)。单击背面后,我需要从数据库中获得最后插入的记录。
我的行包含以下列:question
、optionA
、optionB
、optionC
、optionD
,我需要在Activity
上使用这些数据。我在数据库中创建了一个方法,但它不起作用。
下面是供参考的代码:
MySQLiteHelper.java摘录:
public List<ObjectiveWiseQuestion> getLastInsertQuestion()
{
// long index = 0;
List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
db = getReadableDatabase();
Cursor cursor = db.query(
"sqlite_sequence",
new String[]{"seq"},
"name = ?",
new String[]{TABLE_QUESTION},
null,
null,
null,
null );
if (cursor.moveToFirst())
{
do {
ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();
owq.setQuestion(cursor.getString(2));
owq.setOptionA(cursor.getString(3));
owq.setOptionB(cursor.getString(4));
owq.setOptionC(cursor.getString(5));
owq.setOptionD(cursor.getString(6));
owq.setCorrectOption(cursor.getString(7));
LocwiseProfileList.add(owq);
} while(cursor.moveToNext());
db.close();
}
return LocwiseProfileList;
}
来自AddQuestionActivity.java的OnClickListner
imgBack.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick(View v)
{
msg();
emptyFormField();
try {
final List<ObjectiveWiseQuestion> LocWiseProfile = db.getLastInsertQuestion();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
txtQuestion.setText(cn.getQuestion());
txtOptionA.setText(cn.getOptionA());
txtOptionB.setText(cn.getOptionB());
txtOptionC.setText(cn.getOptionC());
txtOptionD.setText(cn.getOptionD());
txtCorrectOption.setText(cn.getCorrectOption());
db.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
});
请给我一些提示。
发布于 2013-04-17 01:54:09
我认为最上面的答案有点冗长,就用下面这个
SELECT * FROM table ORDER BY column DESC LIMIT 1;
发布于 2012-03-28 14:56:05
要从表中获取最后一条记录..
String selectQuery = "SELECT * FROM " + "sqlite_sequence";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
发布于 2018-12-27 23:39:57
下面是一个简单的示例,它只返回最后一行,而不需要对任何列中的任何内容进行排序:
"SELECT * FROM TableName ORDER BY rowid DESC LIMIT 1;"
https://stackoverflow.com/questions/9902394
复制相似问题