question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After upgrading to SQLDelight 1.4.0 last_insert_rowid() stopped working on iOS.

See original GitHub issue

Runtime Environment SQLDelight version: 1.4.0 Application OS: iOS 13.5.1 Kotlin version 1.3.72

Describe the bug

I use following table

CREATE TABLE IF NOT EXISTS connection (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
start INTEGER,
end INTEGER,
has_error INTEGER AS Boolean DEFAULT 0,
error INTEGER AS Int DEFAULT -1,
message TEXT);

I insert rows like this

insertConnection:
INSERT INTO connection(start, end, has_error, error, message) VALUES (?, ?, ?, ?, ?);

With SQLDelight 1.3.0 I could query the last row like this:

getLastInsertedConnectionId:
SELECT last_insert_rowid();

Since SQLDelight 1.4.0 the query always returns 0. If I use the query on Android it still works. So I guess the issue is in “com.squareup.sqldelight:native-driver:1.4.0”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
rnstewartcommented, Mar 9, 2021

Aha! transactionWithResult was the missing piece of the puzzle.

Thank you, @benasher44!

2reactions
benasher44commented, Jul 2, 2020

last_insert_rowid() only works on the connection where the write happened. There was a previous bug (in the native driver) where writes that weren’t explicitly inside of a transaction would be run on the connection used for queries, but writes wrapped in an explicit transaction would be run on the connection used for writes (all writes should go to the same connection). In practice, this can cause spurious SQLITE_LOCKED-type errors that are hard to diagnose. This was fixed in 1.4.0, but as a result it means that if you want your last_insert_rowid() query to work properly, you will need to wrap it inside of a transaction to force it onto the writer connection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not resolve com.squareup.sqldelight:runtime:1.1.3
I was solved this problem. First: Apply sqldelight plugin right to all project: apply plugin: 'com.squareup.sqldelight' sqldelight ...
Read more >
Changelog - SQLDelight
New: Update to Kotlin 1.0 final. Fix: Report 'sqldelight' folder structure problems in a non-failing way. Fix: Forbid columns named table_name . Their...
Read more >
SQLCipher and Kotlin Multiplatform Mobile - Touchlab
How we implemented Kotlin Multiplatform Mobile encryption with SQDelight and SQLCipher for Kotlin 1.4.0 and made the iOS side work.
Read more >
Create a multiplatform app using Ktor and SQLDelight – tutorial
This tutorial demonstrates how to use Android Studio to create a mobile application for iOS and Android using Kotlin Multiplatform Mobile with ...
Read more >
Kotlin Multiplatform In-Memory SQLDelight Database for ...
So, calling setUpKoinForTesting() in the setUp function will not work. The iOS testing framework allows sending so-called launch arguments: app.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found