I have a one table question_table
and one ImageButton
(Back). I need to get the last inserted record from the database after clicking on the Back.
我有一个表question_table和一个ImageButton(后面)。我需要在单击Back后从数据库中获取最后插入的记录。
My row contains the following columns: question
, optionA
, optionB
, optionC
, optionD
, and I need the data for use on my Activity
. I create one method in database but it's not working.
我的行包含以下列:question,optionA,optionB,optionC,optionD,我需要在我的Activity上使用的数据。我在数据库中创建了一个方法,但它无法正常工作。
Here is code for reference:
这是代码供参考:
MySQLiteHelper.java extract:
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;
}
OnClickListner
from AddQuestionActivity.java
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();
}
}
});
Please give me some hint.
请给我一些提示。
9 个解决方案
#1
140
Try this:
尝试这个:
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
OR
要么
you can also used following solution:
您还可以使用以下解决方案:
SELECT * FROM tablename ORDER BY column DESC LIMIT 1;
SELECT * FROM tablename ORDER BY列DESC LIMIT 1;
#2
123
I think the top answer is a bit verbose, just use this
我认为最好的答案有点冗长,只需使用它
SELECT * FROM table ORDER BY column DESC LIMIT 1;
#3
22
To get last record from your table..
从你的表中获取最后一条记录..
String selectQuery = "SELECT * FROM " + "sqlite_sequence";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
#4
6
I think it would be better if you use the method query from SQLiteDatabase class inseted of the whole SQL string, which would be:
我认为如果你使用SQLiteDatabase类中的方法查询,而不是整个SQL字符串,那将会更好,这将是:
Cursor cursor = sqLiteDatabase.query(TABLE, allColluns, null, null, null, null, ID +" DESC", "1");
The last two parameters are ORDER BY and LIMIT.
最后两个参数是ORDER BY和LIMIT。
You can see more at: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
您可以在以下网址查看更多信息:http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
#5
5
If you have already got the cursor, then this is how you may get the last record from cursor:
如果您已经获得了光标,那么这就是您从光标获取最后一条记录的方法:
cursor.moveToPosition(cursor.getCount() - 1);
//then use cursor to read values
#6
1
Suppose you are looking for last row of table dbstr.TABNAME, into an INTEGER column named "_ID" (for example BaseColumns._ID), but could be anyother column you want.
假设您正在查找表dbstr.TABNAME的最后一行到名为“_ID”的INTEGER列(例如BaseColumns._ID),但可能是您想要的任何其他列。
public int getLastId() {
int _id = 0;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(dbstr.TABNAME, new String[] {BaseColumns._ID}, null, null, null, null, null);
if (cursor.moveToLast()) {
_id = cursor.getInt(0);
}
cursor.close();
db.close();
return _id;
}
#7
1
Just simple, you can move with Cursor
moveToLast(); method provides to move to the last record
简单来说,你可以使用Cursor moveToLast()移动;方法提供移动到最后一条记录
cursor.moveToLast();
#8
1
I wanted to maintain my table while pulling in one row that gives me the last value in a particular column in the table. I essentially was looking to replace the LAST()
function in excel and this worked.
我想保持我的表,同时拉出一行,给我表中特定列的最后一个值。我基本上是想在excel中替换LAST()函数,这很有效。
, (Select column_name FROM report WHERE rowid = (select last_insert_rowid() from report))
#9
0
in sqlite, there is a table called sqlite_sequence, this table contains the table name and it's last id number (if the id is auto incremented).
在sqlite中,有一个名为sqlite_sequence的表,该表包含表名和它的最后一个id号(如果id是自动递增的)。
So, to get the last row in a table just put :
因此,要获取表中的最后一行,只需:
Select * from TABLENAME where id=(SELECT * from sqlite_sequence where name ='TABLENAME')
#1
140
Try this:
尝试这个:
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
OR
要么
you can also used following solution:
您还可以使用以下解决方案:
SELECT * FROM tablename ORDER BY column DESC LIMIT 1;
SELECT * FROM tablename ORDER BY列DESC LIMIT 1;
#2
123
I think the top answer is a bit verbose, just use this
我认为最好的答案有点冗长,只需使用它
SELECT * FROM table ORDER BY column DESC LIMIT 1;
#3
22
To get last record from your table..
从你的表中获取最后一条记录..
String selectQuery = "SELECT * FROM " + "sqlite_sequence";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToLast();
#4
6
I think it would be better if you use the method query from SQLiteDatabase class inseted of the whole SQL string, which would be:
我认为如果你使用SQLiteDatabase类中的方法查询,而不是整个SQL字符串,那将会更好,这将是:
Cursor cursor = sqLiteDatabase.query(TABLE, allColluns, null, null, null, null, ID +" DESC", "1");
The last two parameters are ORDER BY and LIMIT.
最后两个参数是ORDER BY和LIMIT。
You can see more at: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
您可以在以下网址查看更多信息:http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
#5
5
If you have already got the cursor, then this is how you may get the last record from cursor:
如果您已经获得了光标,那么这就是您从光标获取最后一条记录的方法:
cursor.moveToPosition(cursor.getCount() - 1);
//then use cursor to read values
#6
1
Suppose you are looking for last row of table dbstr.TABNAME, into an INTEGER column named "_ID" (for example BaseColumns._ID), but could be anyother column you want.
假设您正在查找表dbstr.TABNAME的最后一行到名为“_ID”的INTEGER列(例如BaseColumns._ID),但可能是您想要的任何其他列。
public int getLastId() {
int _id = 0;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(dbstr.TABNAME, new String[] {BaseColumns._ID}, null, null, null, null, null);
if (cursor.moveToLast()) {
_id = cursor.getInt(0);
}
cursor.close();
db.close();
return _id;
}
#7
1
Just simple, you can move with Cursor
moveToLast(); method provides to move to the last record
简单来说,你可以使用Cursor moveToLast()移动;方法提供移动到最后一条记录
cursor.moveToLast();
#8
1
I wanted to maintain my table while pulling in one row that gives me the last value in a particular column in the table. I essentially was looking to replace the LAST()
function in excel and this worked.
我想保持我的表,同时拉出一行,给我表中特定列的最后一个值。我基本上是想在excel中替换LAST()函数,这很有效。
, (Select column_name FROM report WHERE rowid = (select last_insert_rowid() from report))
#9
0
in sqlite, there is a table called sqlite_sequence, this table contains the table name and it's last id number (if the id is auto incremented).
在sqlite中,有一个名为sqlite_sequence的表,该表包含表名和它的最后一个id号(如果id是自动递增的)。
So, to get the last row in a table just put :
因此,要获取表中的最后一行,只需:
Select * from TABLENAME where id=(SELECT * from sqlite_sequence where name ='TABLENAME')