Coder Social home page Coder Social logo

Comments (5)

Matcha-xiaobin avatar Matcha-xiaobin commented on June 22, 2024

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

安卓端也会报错no such table

from sqllin.

Matcha-xiaobin avatar Matcha-xiaobin commented on June 22, 2024

插入到数据库

    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

from sqllin.

qiaoyuang avatar qiaoyuang commented on June 22, 2024

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

from sqllin.

qiaoyuang avatar qiaoyuang commented on June 22, 2024

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

截屏2024-01-22 22 30 57

from sqllin.

Matcha-xiaobin avatar Matcha-xiaobin commented on June 22, 2024

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

from sqllin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.