亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從sqlite數據庫中獲取總量值

如何從sqlite數據庫中獲取總量值

慕尼黑8549860 2023-06-14 13:56:27
我正在嘗試為計費系統創建一個應用程序。這些值被插入到數據庫中并成功檢索。另外,我需要數據庫中的總金額。數據庫輔助類public Cursor gettotal()    {        SQLiteDatabase database = this.getReadableDatabase();        Cursor cursor =database.rawQuery("select * from '"+PRODUCTS+"' ;", null);        Log.d(TAG, "gettotal: "+cursor.getCount());        return cursor;    }總計private void getTotal() {int m=0;        Cursor cursor = db.gettotal();        if(cursor.getCount()==0)        {            Toast.makeText(getApplicationContext(),"No message is available", Toast.LENGTH_LONG).show();        }        else        {            for(int k = 0; k<=cursor.getCount();k++) {                m += (int) Integer.parseInt(String.valueOf(cursor.getColumnIndex("amount")));            }            i=m;            total_amount.setText(m);        }}我的日志貓D/MainActivity: getTotal: -1E/uetooth_embade: Invalid ID 0x00000008.D/AndroidRuntime: Shutting down VME/AndroidRuntime: FATAL EXCEPTION: main    Process: com.example.bluetooth_embaded, PID: 14063    android.content.res.Resources$NotFoundException: String resource ID #0x8        at android.content.res.Resources.getText(Resources.java:360)        at android.content.res.MiuiResources.getText(MiuiResources.java:97)        at android.widget.TextView.setText(TextView.java:5837)        at com.example.bluetooth_embaded.MainActivity.getTotal(MainActivity.java:100)        at com.example.bluetooth_embaded.MainActivity.access$200(MainActivity.java:33)        at com.example.bluetooth_embaded.MainActivity$5.onClick(MainActivity.java:181)        at android.view.View.performClick(View.java:6616)        at android.view.View.performClickInternal(View.java:6593)        at android.view.View.access$3100(View.java:785)I/Process: Sending signal. PID: 14063 SIG: 9
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

刪除表名稱周圍的單引號:


Cursor cursor = database.rawQuery("select * from " + PRODUCTS +";", null);

需要單引號來包裹字符串文字。

還要用 while 循環替換 for 循環(上限錯誤):


private void getTotal() {

    int m=0;

    Cursor cursor = db.gettotal();

    if (cursor.getCount() == 0) {

        Toast.makeText(getApplicationContext(),"No message is available", Toast.LENGTH_LONG).show();

    } else {

        while (cursor.moveToNext()) {

            m += cursor.getInt(cursor.getColumnIndex("amount"));

        }

        total_amount.setText("" + m);

    }

}

我更換了:


String.valueOf(cursor.getColumnIndex("amount"))

它返回列的索引位置:


cursor.getInt(cursor.getColumnIndex("amount"))

它返回列的值。

我也更換了:


total_amount.setText(m);

這是問題的根源,因為它m是一個整數,被視為資源而不是文本,具有:


total_amount.setText("" + m);

此外,如果您想要的只是列的總和,amount您可以執行此查詢:


Cursor cursor =database.rawQuery("select sum(amount) as total from " + PRODUCTS + ";", null);

所以你只需要得到這個查詢返回的唯一的行和列,你不需要任何循環來計算總數。


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號