2012-09-10 96 views
1

我的问题是关于如何得到一个表的最大ID行MAX ID行......我使用最多的功能,但给我一个错误找张桌子

enter image description here

这里是我的代码

 public static long getLastIdQuotaAdded(Context context){ 
    long id; 
    Cursor cursor; 
    String selection = null; 
    String[] selectionArgs = null; 

    selection = "MAX(" + C_ID + ")"; 

    cursor=context.getContentResolver().query(URI, ALL_COLUMNS, selection, selectionArgs, null); 
    id = cursor.getLong(cursor.getColumnIndex(C_ID)); 
    return id; 
} 

谢谢你YOUT帮助... :)

+0

你可以复制/过去的错误?你的照片在中间被切断... – florianmski

回答

1

您所查询的(甚至是部分可看到)是不是有效的SQL。

获取特定列的最大值,使用是这样的:

SELECT MAX(_id) FROM mytable; 

在SQLite的,如果你的ID是行ID(见the documentation),你可以这样做:

SELECT last_insert_rowid(); 
3

有一个名为sqlite_sequence的表SQLITE用于存储auto_increment_key

这是查询得到最新的auto_increment_key值。

Cursor cursor = db.rawQuery("SELECT seq FROM sqlite_sequence WHERE name=?", 
       new String[] { TABLE_NAME }); 
int last = (cursor.moveToFirst() ? cursor.getInt(0) : 0); 
0

这里是我的解决方案,使表的最大ID ....

public static long getLastIdQuotaAdded(Context context){ 

    Cursor cursor; 
    String selection = null; 
    String[] selectionArgs = null; 
    selection = "SELECT MAX("+C_ID+") FROM sessaoquota"; 

    cursor=context.getContentResolver().query(URI, ALL_COLUMNS, selection, selectionArgs, null); 

    if(cursor.moveToFirst()) 
     do{ 
      idMax = cursor.getInt((0)); 
     }while(cursor.moveToNext()); 

    return idMax; 
}