Search code examples
androidkotlinkotlin-multiplatformsqldelight

SQLDelight multiple table creation


I am experimenting with KMM but I have an issue with SQLDelight. Here is the code sample of the AppDatabase.sq file.

CREATE TABLE Data (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL
);

-- ====>> Add this new table <<==== (QUESTION 1)
CREATE TABLE Settings (
version TEXT NOT NULL
);

insertItem:
INSERT INTO Data(id, content) VALUES(?,?);

removeAll:
DELETE FROM Data;

selectById:
SELECT * FROM Data WHERE id = ?;

updateById:
UPDATE Data SET content = ? WHERE id = ?;

-- ====>> I want this to be run on DB creation <<==== (QUESTION 2)
INSERT INTO Settings(version) VALUES ('1.0.0');

getVersion:
SELECT version FROM Settings;

When I run the application I receive: android.database.sqlite.SQLiteException: no such table: Settings (code 1 SQLITE_ERROR): , while compiling: SELECT version FROM Settings.

If I open the databases created by the app using the App Inspection of Android Studio it seems that only Data table exist and other autogenerated tables which have nothing to do with my tables.

My 2 questions (see comments in SQL code for number reference) are:

  1. How can I create this table (Settings) from .sq file?
  2. How can insert a new value into my table without call any function from actual code?

In case you need the whole project is here but in a previous version because the changes I want to implement are not working!


Solution

  • I have cloned your Github project, added the code you provided here and it works just fine. Both of the tables get created and the Settings table has "1.0.0" version row.

    Have you uninstalled the application on the phone and installed it again? It's possible you're still using the old version of the database since you haven't provided any migrations for SQLDelight to do.

    compiled code