android - 没有从数据库中获取行。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)" +
")"
还有其他想法吗?提前致谢!
解决方案
推荐阅读
- r - 用于应用函数并将输出作为数据帧写入 r 的 for 循环
- git - 在 Git 中,如何在指定标签处获取给定文件的哈希?
- c# - C# Lambda 表达式 - 压缩和/或逻辑
- java - 如何测试在jMock中没有调用类的所有方法?
- java - Sparql- 选择查询作为输入并将其转换为“构造”查询。?
- node.js - Kubernetes 上 Pod 中的 localhost
- r - ggplot2:关于尺寸图例
- ms-access - MS Access\Jet SQL - 员工上班和下班时间、同一张表、每位员工的全职工作总和
- java - Java Web 应用程序无法连接远程 Tomcat 8 服务器上的数据库
- sql - 选择字符串中包含的所有子字符串