如何在不知道 ID 的情况下 select SQLIte 数据库的最后一个条目?
How to select the last entry of an SQLIte DB without knowing the id?
我目前在 android 上使用 SQLite 数据库。
我有一个包含 4 列的简单数据库 (id, c1, c2, c3)
我有一个方法 returns 特定列的最后一个条目。我不知道条目的 ID,但我知道它总是最新的。
目前我是这样做的:
public int Select(String column){
Log.i(TAG, column +" request");
SQLiteDatabase db = this.getReadableDatabase();
Cursor cs = db.query(table, new String[]{column,null,null,null},null,null,null,null,null);
if(cs!=null && cs.moveToFirst()){
cs.moveToLast();
return Integer.parseInt(cs.getString(cs.getColumnIndex(column)));
}
}
在运行时,cs 始终为 null,我不明白为什么。
我做错了什么?
提前致谢
如果你想获得 table 中的最后一个条目,那么你必须有一个列来指示行的插入顺序,比如像 created_at
这样的日期时间列,然后你可以做:
SELECT columnname FROM tablename ORDER BY created_at DESC LIMIT 1
如果没有这样的列,那么您可以使用列 id
,但前提是您已将其定义为 INTEGER PRIMARY KEY AUTOINCREMENT
,(即使没有关键字 AUTOINCREMENT
无法确定顺序,因为 id 可能在删除和插入后被重用):
SELECT columnname FROM tablename ORDER BY id DESC LIMIT 1
所以你的代码必须是:
public Integer select(String columnName) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT " + columnName + " FROM " + tableName + " ORDER BY id DESC LIMIT 1";
Cursor cs = db.rawQuery(sql, null);
Integer result = null;
if(cs.moveToFirst()) {
try {
result = cs.getInt(0); // the query returns only 1 column so it is safe to use its index
} catch (Exception e) {
Log.i(TAG, columnName + " Invalid integer value");
}
}
cs.close();
db.close();
return result;
}
你可以像这样使用上面的方法:
Integer value = select("yourColumnName");
如果 table 为空或值 returned 不是有效整数,它将 return 最后一个条目或 null
。
我使用 Integer
作为方法的 return 类型 select()
因为你也用它。
如果该列具有不同的数据类型,您必须相应地进行更改。
我目前在 android 上使用 SQLite 数据库。 我有一个包含 4 列的简单数据库 (id, c1, c2, c3)
我有一个方法 returns 特定列的最后一个条目。我不知道条目的 ID,但我知道它总是最新的。
目前我是这样做的:
public int Select(String column){
Log.i(TAG, column +" request");
SQLiteDatabase db = this.getReadableDatabase();
Cursor cs = db.query(table, new String[]{column,null,null,null},null,null,null,null,null);
if(cs!=null && cs.moveToFirst()){
cs.moveToLast();
return Integer.parseInt(cs.getString(cs.getColumnIndex(column)));
}
}
在运行时,cs 始终为 null,我不明白为什么。 我做错了什么?
提前致谢
如果你想获得 table 中的最后一个条目,那么你必须有一个列来指示行的插入顺序,比如像 created_at
这样的日期时间列,然后你可以做:
SELECT columnname FROM tablename ORDER BY created_at DESC LIMIT 1
如果没有这样的列,那么您可以使用列 id
,但前提是您已将其定义为 INTEGER PRIMARY KEY AUTOINCREMENT
,(即使没有关键字 AUTOINCREMENT
无法确定顺序,因为 id 可能在删除和插入后被重用):
SELECT columnname FROM tablename ORDER BY id DESC LIMIT 1
所以你的代码必须是:
public Integer select(String columnName) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT " + columnName + " FROM " + tableName + " ORDER BY id DESC LIMIT 1";
Cursor cs = db.rawQuery(sql, null);
Integer result = null;
if(cs.moveToFirst()) {
try {
result = cs.getInt(0); // the query returns only 1 column so it is safe to use its index
} catch (Exception e) {
Log.i(TAG, columnName + " Invalid integer value");
}
}
cs.close();
db.close();
return result;
}
你可以像这样使用上面的方法:
Integer value = select("yourColumnName");
如果 table 为空或值 returned 不是有效整数,它将 return 最后一个条目或 null
。
我使用 Integer
作为方法的 return 类型 select()
因为你也用它。
如果该列具有不同的数据类型,您必须相应地进行更改。