我在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
在cursor.count
中显示0。
我知道表中有行,因为当我运行下一个代码时,它会带出我要查找的行:
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"
(没关系,我的表只有一行)。但是,当我添加第二部分(在////////之后)时,显示错误: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)" +
")"
还有其他想法吗?预先感谢!
答案 0 :(得分:1)
在rawquery
中尝试整个语句:
val sql = "select * from $TABLE_INVENTORY WHERE trim($COL_PRODUCTID) = '$barcode'"
val cursor = p0.rawQuery(sql, null)
我认为条形码是TEXT
答案 1 :(得分:1)