Skip to content

Fix SQL Upsert Compatibility Issue for Android 10 and Lower#106

Closed
brianabdl wants to merge 5 commits intoIReaderorg:masterfrom
brianabdl:fix-sql-chapters
Closed

Fix SQL Upsert Compatibility Issue for Android 10 and Lower#106
brianabdl wants to merge 5 commits intoIReaderorg:masterfrom
brianabdl:fix-sql-chapters

Conversation

@brianabdl
Copy link
Copy Markdown
Contributor

Using upsert when inserting non-existing chapters the app shows an error. When I change it to insert, it works just fine. I don't really understand SQL, but this fixes the issue.

I've also collected some logs, if my changes are not a desired behavior please give some advice xD

ExceptionString(e=android.database.sqlite.SQLiteException: near "ON": syntax error (code 1 SQLITE_ERROR): , 
while compiling: INSERT INTO chapter(_id,book_id,url,name,scanlator,read,bookmark, last_page_read,chapter_number,source_order,date_fetch,date_upload,content,type)
VALUES (?, ?, ?,?,?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(_id)
DO UPDATE
SET
url = ?,
read = ?,
bookmark = ?,
last_page_read = ?,
content = ?,
type = ?

WHERE _id IS ?)

@kazemcodes
Copy link
Copy Markdown
Member

I use upsert in order to update chapters when it exist and if it doesn't exist then just insert a new chapter into db

@kazemcodes
Copy link
Copy Markdown
Member

when this error happens and what is your phone model?

@brianabdl
Copy link
Copy Markdown
Contributor Author

Got the idea, but somehow when it try insert a new chapters, it shows an error message

@kazemcodes
Copy link
Copy Markdown
Member

kazemcodes commented Oct 10, 2024

I tried to insert chapter using a samsung device with android 11 and then nothing happed, there wasn't an error , even app doesn't crashed

@brianabdl
Copy link
Copy Markdown
Contributor Author

I'm using Android 10, maybe the SQLite version on the device is causing this issue,
As mentioned in Android SQLite Javadoc, Android 9-10 is still using an SQLite version 3.22

The phrase "ON CONFLICT" is also part of UPSERT, which is an extension to INSERT added in version 3.24.0 (2018-06-04).

@brianabdl
Copy link
Copy Markdown
Contributor Author

brianabdl commented Oct 11, 2024

Found the solution, we can use INSERT OR REPLACE to fix compatibility issues.

@brianabdl brianabdl changed the title Fix SQL error when insertChapters Fix SQL Upsert Compatibility Issue for Android 10 and Lower Oct 11, 2024
@kazemcodes
Copy link
Copy Markdown
Member

Insert or Replace are very different from using Insert ..... Do Update Set

@kazemcodes
Copy link
Copy Markdown
Member

it's better to not change the current database queries

@kazemcodes
Copy link
Copy Markdown
Member

Insert or Replace are very different from using Insert ..... Do Update Set

when operation use replace, a completely new object is created in database that is completely unrelated to the previous one and because of that is cause a very serious issue in lazyColumn widget

@brianabdl
Copy link
Copy Markdown
Contributor Author

Hmm, it's true, maybe use a conditional statement to imitate upsert behavior?

@kazemcodes
Copy link
Copy Markdown
Member

I tried on android 7 device and there was not an issue or break in app functionality?
which android device do you have problems with

@brianabdl
Copy link
Copy Markdown
Contributor Author

Android 10, Poco X3 NFC

@brianabdl
Copy link
Copy Markdown
Contributor Author

brianabdl commented Oct 13, 2024

Just tested the lastest release(v0.1.38), seems fine.
I only get SQL error when I try to build the project by myself, I'm pretty sure everything is correct.
Don't know what's wrong with it.

@kazemcodes kazemcodes closed this Mar 26, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 29, 2025
@brianabdl brianabdl deleted the fix-sql-chapters branch March 30, 2025 12:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants