0

I implemented a Serializable interface in my custom class so that I would be able to store it to SQLite database, but when I create the values to be stored to the database it basically complains that my put() call doesn't have appropriate arguments:

"None of the following functions can be called with the arguments supplied:" error.

val values = ContentValues().apply {
            put("amount", expense.amount)
            put("date", expense.date)
            put("category", expense.category)
        }
database.insert("entries", null, values)

The issue is with put("date", expense.date) : the expense.date it is my custom object.

I'm guessing I need to implement a function in my custom class which will convert its fields into ByteArray and return that ByteArray?

ShrikeThe
  • 77
  • 6

1 Answers1

0

The ContentValues's put methods are limited to types Short,Long,Double,Integer,String,Boolean,Float,Byte[] and Byte.

Converting to Byte[] is probably not the best option. If you converted to a Long or String respecting the SQLite's formats (see link) then the stored value could be more useful as you could use the SQLite built in date and time functions, whilst storing as a blob (byte[]) has limitations and is likely to be more complex.

You could easily add additional functions in your custom class to convert the date accordingly.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Oh cool, I didn't know about these built-ins, thx! Ok, so ContentValues has problems with dealing with the custom objects but I am now beginning to wonder....since I implemented Serializable interface in my class how do I actually go about inserting this object into the sqlite database? I could just call the SQL insert command and store it as a BLOB right? – ShrikeThe Mar 10 '21 at 22:31
  • @ShrikeThe Object to byte array (perhaps as per https://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array), then use content values put as opposed to trying to put the object. Retrieve from Cursor into a byte array using Cursor's getBlob method. Note that if the byte arrays is near to 4M then get will fail. Not really recommend storing large byte arrays, consider up to 100-200k otherwise performance may be an issue. – MikeT Mar 10 '21 at 23:17
  • Ah ok, I get it. Thx! – ShrikeThe Mar 11 '21 at 18:50