I have problem with saving data to sqldelight 1.4 in Android app.
I created table:
CREATE TABLE myTable(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT
);
In repo class I have insert function with myTable object as an argument
fun insert(item: myTable) {
myTableQueries.insert(
name = item.name
)
}
And in some action i try to create new object Table with name but without id, as id is auto incremented. I had not found any info how to do that. Which is the best solution?
myTableViewModel.insert(name)
@Parcelize
class myTable(val name: String) : Parcelable
and in viewmodel use params?
Thanks
This topic is already discusssed in sqldelight issue here: https://github.com/cashapp/sqldelight/issues/1284
You can define your sqlite insert
statement in your .sq file using ?
parameters for all the variables, as the doc of sqldelight states: https://cashapp.github.io/sqldelight/android_sqlite/
Example:
CREATE TABLE hockeyPlayer (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_number INTEGER NOT NULL,
full_name TEXT NOT NULL
);
insert:
INSERT INTO hockeyPlayer
VALUES (?, ?, ?);
And from your Kotlin code you can pass null
for its id, which makes auto-incrementation work:
query.insert(
id = null,
player_number = 100,
full_name = "name"
)