0

我正在尝试将我插入数据库的最新行放入游标中。我尝试了多种类型的查询,但似乎没有任何效果。

,这是我的代码:

public Expense createExpense(Expense expense) {
    ContentValues values = new ContentValues();
    values.put(ExpDataBase.COLUMN_DESCRIPTION, expense.getDesc());
    values.put(ExpDataBase.COLUMN_amount, expense.getAmount());
    values.put(ExpDataBase.COLUMN_DATE, expense.getDate().toString());
    values.put(ExpDataBase.COLUMN_category, expense.getCategory());

    Integer insertId =(int) database.insert(ExpDataBase.TABLE_NAME, null,
            values);    

    String id="'"+insertId.toString()+"'";

    Cursor c = database.query(ExpDataBase.TABLE_NAME, null,
            null, null, null, null, null);
int i=  c.getCount();


    Cursor cursor= database.query(ExpDataBase.TABLE_NAME, null,ExpDataBase.COLUMN_ID+"="+id, null, null, null, null);

*插入方法有效,我已经看到数据库表中的行。*getCount 返回与 insertId 相同的数字

但是当我尝试将最后一行放入游标时,我什么也没得到,我在游标中看到它在行数中返回-1。

我迷路了。请帮我 :(

4

2 回答 2

0

如果我没记错的话,您的查询有问题

String[] fields = new String[]{ExpDataBase.COLUMN_ID};
Cursor cursor= database.query(ExpDataBase.TABLE_NAME, fields,ExpDataBase.COLUMN_ID+"="+id, null, null, null, null);
于 2013-08-01T00:07:53.450 回答
0
public Expense createExpense(Expense expense) {
ContentValues values = new ContentValues();
values.put(ExpDataBase.COLUMN_DESCRIPTION, expense.getDesc());
values.put(ExpDataBase.COLUMN_amount, expense.getAmount());
values.put(ExpDataBase.COLUMN_DATE, expense.getDate().toString());
values.put(ExpDataBase.COLUMN_category, expense.getCategory());

long insertId = database.insert(ExpDataBase.TABLE_NAME, null,
        values);    

String id="'"+insertId.toString()+"'";

Cursor c = database.query(ExpDataBase.TABLE_NAME, null,
        null, null, null, null, null);
int i=  c.getCount();


Cursor cursor= database.query(ExpDataBase.TABLE_NAME, null,ExpDataBase.COLUMN_ID+"=?", new String[]{String.valueOf(insertId)}, null, null, null);
于 2013-08-01T00:16:04.313 回答