Working with Queries of SQLDelight 2.0.1 failed

99 Views Asked by At

I want to get familiar with the SQLDelight Plugin but I get an error when I try to access my queries in the database.

I followed the getting started tutorial of the official website -> https://cashapp.github.io/sqldelight/2.0.1/android_sqlite/

So I started by using an empty compose Activity in Android Studio and added the following plugin to the build.gradle.kts (:app)-File:

plugins {
  id("app.cash.sqldelight") version "2.0.1"
}

after the successful build I added the SqlDelightExtension with:

sqldelight {
    databases {
        create("Database") {
            packageName.set("aut.patrick.reschi")
        }
    }
}

after another successful build I defined my schema under src/main/sqldelight/aut/patrick/reschi/Player.sq

CREATE TABLE hockeyPlayer (
  player_number INTEGER PRIMARY KEY NOT NULL,
  full_name TEXT NOT NULL
);

CREATE INDEX hockeyPlayer_full_name ON hockeyPlayer(full_name);

INSERT INTO hockeyPlayer (player_number, full_name)
VALUES (15, 'Ryan Getzlaf');

That results into an automatic build as mentioned in the documentation. However, there is a weird behaviour that in the generated Database-Interface under app/build/generated/sqldelight/code/Database/debug/aut/patrick/reschi/ are unresolved imports. These errors only appear in the debug module, not in the "release" directory. Screenshot for better understanding.

generated Database-Interface File

The behaviour above might be related to the following problem. To access the queries you need to have a first have to add the following dependency:

implementation("app.cash.sqldelight:android-driver:2.0.1")

and after implementing a simple query in the Player.sq File, like for instance this one

selectAll:
SELECT *
FROM hockeyPlayer;

the plugin generate another class, in this case "PlayerQueries".

Now you should be able to create instances for your driver and database and therefore also get access to the queries.

In my case I just did that all in the MainActivity.kt File but that hopefully couldn't be the problem. So I tried:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val driver = AndroidSqliteDriver(Database.Schema, applicationContext, "test.db")
            val database = Database(driver)
            val playerQueries : PlayerQueries = database.playerQueries

        }
    }
}

But the "playerQueries" in the database cannot be found for some reason. I took a screenshot from the possible choices from the database instance:

Choices from database instance


Update: 15.02.2024:

I ran into the same issue with my Windows PC, so it's definitely not a specific workstation problem.

I always think about, that it could be a package name problem, but I already tried it like 40 times with different package names and database directory names, but it is always the same.

0

There are 0 best solutions below