当从数据库获取数据时仅获取行的第一个索引时,我想要每行的所有数据

我是 android 和 java 新手...想要从 sqlite 中的每一行获取所有数据。


public Cursor fetch() {

        dh = this.getReadableDatabase();

        Cursor cursor = this.dh.query(TABLE_INVOICE, new String[]{ITEM_NAME, ITEM_PRICE, QTY, ITEM_TAX, BILL_NO, DATE, CUST_NAME, CUST_ADDR, INVOICE_TOT, GRAND_TOT, TIME}, null, null, null, null, null);

        if (cursor != null) {

            cursor.moveToFirst();

        }

    return cursor;

}

数据获取:


Cursor cursor = datahandler.fetch();

//                        String response = datahandler.getInvoice(itemName,invoice,mprice,billno,mdate,qty);

        ITEM_NAME = cursor.getString(0).trim();

        ITEM_PRICE = cursor.getString(1).trim();

        QTY = cursor.getString(2).trim();

        ITEM_TAX = cursor.getString(3).trim();

        BILL_NO = cursor.getString(4).trim();

        DATE = cursor.getString(5).trim();

        CUST_NAME = cursor.getString(6).trim();

        CUST_ADDR = cursor.getString(7).trim();

        INVOICE_TOT = cursor.getString(8).trim();

        GRAND_TOT = cursor.getString(9).trim();

        TIME = cursor.getString(10).trim();


芜湖不芜
浏览 102回答 2
2回答

鸿蒙传说

重复调用cursor.moveToNext()直到返回false,并提取其间的数据。Cursor cursor = datahandler.fetch();do {    // extract data from cursor for current row} while (cursor.moveToNext());为了防止出现空值,Cursor您还应该调用 -- 并测试 -- 的返回值moveToFirst()。moveToFirst()您在检索 时调用Cursor,但从未测试结果。if (cursor.moveToFirst()) {    do {        // extract data from cursor for current row    } while (cursor.moveToNext());}

当年话下

从数据库返回列表中的所有项目。public List<Records> fetch() {&nbsp; &nbsp; List<Records> records = new ArrayList<Records>();&nbsp; &nbsp; dh = this.getReadableDatabase();&nbsp; &nbsp; Cursor cursor = this.dh.query(TABLE_INVOICE, new String[]{ITEM_NAME, ITEM_PRICE, QTY, ITEM_TAX, BILL_NO, DATE, CUST_NAME, CUST_ADDR, INVOICE_TOT, GRAND_TOT, TIME}, null, null, null, null, null);&nbsp; &nbsp; if ( cursor.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITEM_NAME = cursor.getString(0).trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITEM_PRICE = cursor.getString(1).trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QTY = cursor.getString(2).trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ITEM_TAX = cursor.getString(3).trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.add(new Records(ITEM_NAME,ITEM_PRICE,QTY,ITEM_TAX));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } while (cursor.moveToNext());&nbsp; &nbsp; }&nbsp; &nbsp; return records;}然后List<Records> records = db.fetch(); 循环记录列表
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java