首页 > 解决方案 > 没有从数据库中获取行。SQLite、安卓、科特林

问题描述

我在 DatabaseHelper.kt 中有一个函数:

fun readData_Inventory(barcode: String): Inventory{ 
        val p0 = this.readableDatabase
        val selection = "$COL_PRODUCTID = ?"
        val selectionArgs = arrayOf(barcode)
        val cursor = p0.query(TABLE_INVENTORY, null, selection, selectionArgs,null, null, null)
        Toast.makeText(context, "Barcode: $barcode, Count: ${cursor.count}", Toast.LENGTH_LONG).show()
        val inventory: Inventory?
        if (cursor.moveToLast()) {
            cursor.moveToLast()
            val productId = cursor.getString(cursor.getColumnIndex(COL_PRODUCTID))
            val quantity = cursor.getDouble(cursor.getColumnIndex(COL_QUANTITY))
            val source = cursor.getString(cursor.getColumnIndex(COL_SOURCE))
            val dateCheck = cursor.getString(cursor.getColumnIndex(COL_DATECHECK))
            inventory = Inventory(productId, quantity, source, dateCheck)
        } else{
            inventory = Inventory ("000", 0.0, "", "")
        }
        return inventory

Toast显示 0 在cursor.count.

我知道该表有行,因为当我运行下一个代码时,它会带来我正在寻找的行:

fun readData_Inventory(barcode: String): Inventory{ 
        val p0 = this.readableDatabase
        val sql = "select * from $TABLE_INVENTORY"
        val cursor = p0.rawQuery(sql, null)
        val inventory: Inventory?
        Toast.makeText(context, "Barcode: $barcode, Count: ${cursor.count}", Toast.LENGTH_LONG).show()
        if (cursor.moveToLast()) {
            cursor.moveToLast()
            val productId = cursor.getString(cursor.getColumnIndex(COL_PRODUCTID))
            val quantity = cursor.getDouble(cursor.getColumnIndex(COL_QUANTITY))
            val source = cursor.getString(cursor.getColumnIndex(COL_SOURCE))
            val dateCheck = cursor.getString(cursor.getColumnIndex(COL_DATECHECK))
            inventory = Inventory(productId, quantity, source, dateCheck)
        } else{
            inventory = Inventory ("000", 0.0, "", "")
        }
        return inventory

任何帮助表示赞赏。提前致谢。

编辑: 我尝试了下一个代码:

fun readData_Inventory(barcode: String) {
        val p0 = this.readableDatabase
        val inventory: Inventory?
        val sql = "select * from $TABLE_INVENTORY"
        val cursor = p0.rawQuery(sql, null)
        Toast.makeText(context, "Size: ${cursor.count} Move: ${cursor.moveToFirst()}", Toast.LENGTH_LONG).show()
        /////////////////
        cursor.moveToFirst()
        val productId = cursor.getString(cursor.getColumnIndex(COL_PRODUCTID))
        Toast.makeText(context, "ID: $productId", Toast.LENGTH_LONG).show()
    }

当我只使用第一部分(在 /////// 部分之前)运行代码时,吐司会显示"Size: 1, true"(没关系,我的表只有 1 行)。但是当我添加第二部分(在 //////// 之后)它显示错误:android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

我正在添加我插入数据的部分......也许是......

fun insertData_Inventory(inventory: Inventory){
        val p0 = this.writableDatabase
        val cv = ContentValues()
        cv.put(COL_PRODUCTID, inventory.product_id)
        cv.put(COL_QUANTITY, inventory.quantity)
        cv.put(COL_SOURCE, inventory.source)
        cv.put(COL_DATECHECK, inventory.checkDate)
        val result = p0.insert(TABLE_INVENTORY, null, cv)
        if (result == (-1).toLong())
            Toast.makeText(context, "Insert Inventory Failed", Toast.LENGTH_SHORT).show()
        else
            Toast.makeText(context, "Insert Inventory Success", Toast.LENGTH_SHORT).show()
        p0.close()
    }

它总是插入并显示“插入库存成功”

还有我声明表格的部分:

val TABLE_INVENTORY = "Inventory"
val COL_INVENTORYID = "inventory_id"
val COL_PRODUCTID = "product_id"
val COL_QUANTITY = "quantity"
val COL_SOURCE = "source"
val COL_DATECHECK = "date_check"
val CREATE_T_INVENTORY = "CREATE TABLE if not exists $TABLE_INVENTORY " +
        "(" +
        "$COL_INVENTORYID integer PRIMARY KEY, " +
        "$COL_PRODUCTID varchar(20), " +
        "$COL_QUANTITY double, " +
        "$COL_SOURCE varchar(20)," +
        "$COL_DATECHECK datetime, " +
        "FOREIGN KEY($COL_PRODUCTID) REFERENCES $TABLE_PRODUCT($COL_PRODUCTID)" +
        ")"

还有其他想法吗?提前致谢!

标签: androidsqlitekotlin

解决方案


初始化对象时我遗漏了一行:

库存.product_id = 产品.product_id

谢谢,尤其是@mTak


推荐阅读