简体   繁体   English

如何在Kotlin / Native中使用SQLite?

[英]How to use SQLite in Kotlin/Native?

For a multi-platform project, I consider Kotlin/Native , with the hope to have a common code base at least for business logic and saving of data. 对于一个多平台项目,我考虑Kotlin / Native ,希望至少有一个共同的代码库用于业务逻辑和数据保存。

For saving of data I would like to use a database like SQLite. 为了保存数据,我想使用像SQLite这样的数据库。 However, according to another stackoverflow thread , if I want to compile to Kotlin/Native, I cannot use any Java file. 但是,根据另一个stackoverflow线程 ,如果我想编译到Kotlin / Native,我不能使用任何Java文件。 If I understand this correctly, it also means that any SQLite library I include is not allowed to import from Java. 如果我理解正确,这也意味着我不允许从Java导入任何包含的SQLite库。

On the search for such a library, I found two interesting database projects by Jetbrain, namely Anko SQLite and Exposed . 在搜索这样一个库时,我发现Jetbrain有两个有趣的数据库项目,即Anko SQLiteExposed However, when digging in their source code, at the time of this writing both import from Java (eg java.sql). 但是,在挖掘源代码时,在编写本文时都要从Java导入(例如java.sql)。 Which probably means I cannot use them. 这可能意味着我不能使用它们。

One idea I had was to use a c-based sqlite library, since according to mentioned thread , Kotlin/Native code is allowed to use c libraries. 我的一个想法是使用基于c的sqlite库,因为根据提到的线程 ,Kotlin / Native代码被允许使用c库。 This may be possible, but I wondered if there isn't a suggested more Kotlin like way of using SQLite or another database in Kotlin/Native? 这可能是可能的,但我想知道是否有更多的Kotlin建议使用SQLite或Kotlin / Native中的其他数据库?

You do need to use the C SQLite library, but most of your code shouldn't work with it directly. 您确实需要使用C SQLite库,但是大多数代码不应该直接使用它。 Instead you write a wrapper which will be the more Kotlin-like way you are asking about. 相反,你写一个包装器,这将是你要问的更像Kotlin的方式。

I haven't seen such a wrapper done as a separate maintained project (there are very few libraries for Kotlin/Native yet), but Jetbrains actually has a basic one in https://github.com/JetBrains/kotlinconf-spinner/tree/master/kotlin-native/samples/fullstack/sql which you can use as base for your own. 我还没有看到这样做作为一个单独的项目保持一个包装(有用于科特林/本地很少库还没有),但实际上Jetbrains的有一个基本的https://github.com/JetBrains/kotlinconf-spinner/tree / master / kotlin-native / samples / fullstack / sql ,您可以将它们用作您自己的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM