光标返回 0

Cursor returning 0

我有一个函数,我想在其中获取 MySQL 数据库 table 行的值的总和。这是我的代码:

public int getSum(int var) {

    int x=0;
    // Select All Query
    String selectQuery = "SELECT sum(amount) FROM donations WHERE aid = '"+aid+"'";

    SQLiteDatabase db = openOrCreateDatabase("shareity", Context.MODE_PRIVATE, null);
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {

            x = cursor.getInt(0);

        } while (cursor.moveToNext());
    }

    // return contact list
    Log.d("id", "Id" + "," + x);
    return x;
}

我在 onCreate() 中这样调用它:

getSum(idk);

其中 idk 是一个整数。但是我的这个 returns 0。我能知道为什么吗? table 的行我想求和,也是整数。

终于想通了。问题出在 SQLite 数据库上。我不得不使用 php 文件将数据库从 mysql 服务器下载到本地 Sqlite 服务器,然后循环遍历它