第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從sqlite數(shù)據(jù)庫(kù)中獲取總量值

如何從sqlite數(shù)據(jù)庫(kù)中獲取總量值

慕尼黑8549860 2023-06-14 13:56:27
我正在嘗試為計(jì)費(fèi)系統(tǒng)創(chuàng)建一個(gè)應(yīng)用程序。這些值被插入到數(shù)據(jù)庫(kù)中并成功檢索。另外,我需要數(shù)據(jù)庫(kù)中的總金額。數(shù)據(jù)庫(kù)輔助類public Cursor gettotal()    {        SQLiteDatabase database = this.getReadableDatabase();        Cursor cursor =database.rawQuery("select * from '"+PRODUCTS+"' ;", null);        Log.d(TAG, "gettotal: "+cursor.getCount());        return cursor;    }總計(jì)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貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊

刪除表名稱周?chē)膯我?hào):


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

需要單引號(hào)來(lái)包裹字符串文字。

還要用 while 循環(huán)替換 for 循環(huán)(上限錯(cuò)誤):


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);

這是問(wèn)題的根源,因?yàn)樗黰是一個(gè)整數(shù),被視為資源而不是文本,具有:


total_amount.setText("" + m);

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


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

所以你只需要得到這個(gè)查詢返回的唯一的行和列,你不需要任何循環(huán)來(lái)計(jì)算總數(shù)。


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 1 回答
  • 0 關(guān)注
  • 294 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)