I have a function inside 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
But the Toast shows 0 in cursor.count.
I know the table has rows because when I run the next code it brings the rows I'm looking for:
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
Any help is appreciated. Thanks in advance.
EDIT: I tried with the next code:
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()
}
When I run the code with only the first part (before the /////// part), the toast shows "Size: 1, true" (that's ok, my table only has 1 row). But when I add the second part (after the ////////) it shows the error: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
I'm adding the part where I insert the data... maybe is that...
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()
}
It always inserts and shows "Insert Inventory Success"
Also the part where I declare the table:
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)" +
")"
Any other ideas? Thanks in advance!
"""so that you don't need to do all this+stuff."""CREATE TABLE if not exists $TABLE_INVENTORY ( $COL_INVENTORYID integer PRIMARY KEY, ...."""