You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KMM集成SQLDelight 2.0.1创建SqliteDriver遇参数匹配错误

SQLDelight 2.0.1多平台集成构造Driver参数错误解决方法

问题描述

按照SQLDelight 2.0.1官方文档集成到Kotlin Multiplatform项目时,构建应用出现以下错误:

Android平台错误

None of the following functions can be called with the arguments supplied: 
public constructor AndroidSqliteDriver(database: SupportSQLiteDatabase, cacheSize: Int = ..., windowSizeBytes: Long? = ...) defined in app.cash.sqldelight.driver.android.AndroidSqliteDriver
private constructor AndroidSqliteDriver(openHelper: SupportSQLiteOpenHelper? = ..., database: SupportSQLiteDatabase? = ..., cacheSize: Int, windowSizeBytes: Long? = ...) defined in app.cash.sqldelight.driver.android.AndroidSqliteDriver
public constructor AndroidSqliteDriver(schema: SqlSchema<QueryResult.Value<Unit>>, context: Context, name: String? = ..., factory: SupportSQLiteOpenHelper.Factory = ..., callback: SupportSQLiteOpenHelper.Callback = ..., cacheSize: Int = ..., useNoBackupDirectory: Boolean = ..., windowSizeBytes: Long? = ...) defined in app.cash.sqldelight.driver.android.AndroidSqliteDriver

iOS平台错误

error: None of the following functions can be called with the arguments supplied: 
public constructor NativeSqliteDriver(schema: SqlSchema<QueryResult.Value<Unit>>, name: String, maxReaderConnections: Int = ..., onConfiguration: (DatabaseConfiguration) -> DatabaseConfiguration = ..., vararg callbacks: AfterVersion) defined in app.cash.sqldelight.driver.native.NativeSqliteDriver
public constructor NativeSqliteDriver(configuration: DatabaseConfiguration, maxReaderConnections: Int = ...) defined in app.cash.sqldelight.driver.native.NativeSqliteDriver
public constructor NativeSqliteDriver(databaseManager: DatabaseManager, maxReaderConnections: Int = ...) defined in app.cash.sqldelight.driver.native.NativeSqliteDriver

参照文档编写的各平台actual方法:

Android代码

actual class DatabaseDriverFactory(private val context: Context) {
    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(AppDatabase.Schema, context, "test.db")
    }
} 

iOS代码

actual class DatabaseDriverFactory {
    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(AppDatabase.Schema, "test.db")
    }
}

实际构造函数需要更多参数,以下是具体解决方法:


解决方案

Android平台

构造函数本身带有默认参数,报错大概率是依赖导入或参数传递方式问题,可通过以下两种方式解决:

方式1:显式传递默认参数值

需先导入androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory:

actual class DatabaseDriverFactory(private val context: Context) {
    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(
            schema = AppDatabase.Schema,
            context = context,
            name = "test.db",
            factory = FrameworkSQLiteOpenHelperFactory(),
            callback = AndroidSqliteDriver.Callback(AppDatabase.Schema),
            cacheSize = 1000,
            useNoBackupDirectory = false,
            windowSizeBytes = null
        )
    }
}

方式2:简化写法(推荐)

使用命名参数明确传递必要参数,利用默认值填充其他参数:

actual class DatabaseDriverFactory(private val context: Context) {
    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(
            schema = AppDatabase.Schema,
            context = context,
            name = "test.db"
        )
    }
}

同时检查Android模块依赖是否正确引入:

// Android模块build.gradle.kts
implementation("app.cash.sqldelight:android-driver:2.0.1")

iOS平台

NativeSqliteDriver的参数同样带有默认值,可通过以下方式处理:

方式1:显式传递默认参数值

actual class DatabaseDriverFactory {
    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(
            schema = AppDatabase.Schema,
            name = "test.db",
            maxReaderConnections = 5,
            onConfiguration = { it },
            callbacks = *emptyArray()
        )
    }
}

方式2:简化写法(推荐)

用命名参数传递必要参数,其余依赖默认值:

actual class DatabaseDriverFactory {
    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(
            schema = AppDatabase.Schema,
            name = "test.db"
        )
    }
}

检查iOS模块依赖是否正确:

// iOS模块build.gradle.kts
implementation("app.cash.sqldelight:native-driver:2.0.1")

关键注意点

  • 确保所有模块的SQLDelight版本统一为2.0.1
  • 检查Driver类的导入包是否正确,避免导入错误路径
  • 推荐使用命名参数,可有效避免参数顺序导致的匹配错误

内容的提问来源于stack exchange,提问作者neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:33:22