Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在桌面平台上没有找到数据库 #75

Closed
Matcha-xiaobin opened this issue Jan 22, 2024 · 5 comments
Closed

在桌面平台上没有找到数据库 #75

Matcha-xiaobin opened this issue Jan 22, 2024 · 5 comments

Comments

@Matcha-xiaobin
Copy link

image
桌面平台上,使用以上创建数据库 ,然后在尝试读取表的时候,报错
Exception in thread "AWT-EventQueue-0" org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: InfoPage)

请问,我是需要采用这个方式手动创建数据库吗?
image

@Matcha-xiaobin
Copy link
Author

还有就是,存放数据库文件的完整目录如果不存在,也会报错,需要我在初始化数据库对象之前,先创建这个目录,文档里并没有说明这些。

安卓端也会报错no such table

@Matcha-xiaobin
Copy link
Author

插入到数据库

    internal val database by lazy {
        Database(name = "MyDatabase.db", path = getGlobalDatabasePath(), version = 1)
    }

    /**
     * 插入数据
     */
    suspend fun insert(infoData: InfoPageData) {
        if (findByName(infoData.name) != null) {
            update(infoData)
            return
        }
        database suspendedScope {
            InfoPageDataTable { table ->
                table INSERT infoData
            }
        }
    }

    @DBRow(tableName = "InfoPage")
@Serializable
data class InfoPageData(
    val pageUrl: String,
    val name: String,
    val otherName: String,
    val img: String,
    val upTime: String,
    val area: String,
    val mIndex: String,
    val type: String,
    val flag: String,
    val summary: String,
    val dataType: Int = 0,
)

桌面端数据库文件路径

val databaseDir: File
    get() = File(
        System.getProperty("user.home"),
        when (os) {
            OS.MacOS -> {
                "Library${separator}Application Support${separator}ComposeApp${separator}database"
            }

            OS.Windows -> {
                "AppData${separator}Local${separator}ComposeApp${separator}database"
            }

            else -> {
                ".ComposeApp${separator}database"
            }
        }
    )

actual fun getGlobalDatabasePath(): DatabasePath {
    val path = databaseDir.absolutePath
    val file = File(databaseDir.absolutePath)
    if (!file.exists()) {
        file.mkdirs()
    }
    return path.toDatabasePath()
}

Gradle:

// KSP dependencies
dependencies {
    // sqllin-processor
    add("kspCommonMainMetadata", "com.ctrip.kotlin:sqllin-processor:1.2.4")
}

afterEvaluate {
    // WORKAROUND: both register() and named() fail – https://github.com/gradle/gradle/issues/9331
    tasks {
        withType<KotlinCompile<*>> {
            if (name != "kspCommonMainKotlinMetadata")
                dependsOn("kspCommonMainKotlinMetadata")
        }
    }
}

其它:
Java 18
kotlin 1.9.21
ksp 1.9.21-1.0.15

@qiaoyuang
Copy link
Collaborator

你文档里面的截图都找到了,表当然需要自己创建。还有文件路径的问题,传入一个可用的路径当然也是使用者需要确保的,自动创建路径一旦遇到非法路径会碰到更多问题。还有,基于文档原则,如果 SQLlin 会自动帮用户完成某事会在文档中说明,否则则是文档 bug。用户不应该期待一个库会做到文档中没有提到的事情。

@qiaoyuang
Copy link
Collaborator

qiaoyuang commented Jan 22, 2024

另外需要补充的是,对于“会改变数据库结构的操作“,文档中已经进行了说明,请仔细阅读文档:

截屏2024-01-22 22 30 57

@Matcha-xiaobin
Copy link
Author

我是直接从jetpack room过来的,并不知道首次创表是需要写sql的,你文档也不是写的首次-创表需要在create里写sql,因为你那儿写的是可以采用更多配置来创建数据库,而不是必须要通过这种方式首次完成创表。我的想法是你都支持给data class加dbrow注解了,应该和room一样自动完成创表。
当然,这些都不重要了,现在我知道了。
感谢你花时间回答我。
另外我还是建议一下你文档在写明白点,我觉得今后也许有不少人会跟我一样遇到这情况。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants