作者:姑另静乖_606 | 来源:互联网 | 2022-12-31 16:54
此代码直接从Kotlin公开的Wiki中提取,但不起作用。奇怪而令人沮丧的是,我无法实现它,因为我有一个很棒的项目,需要使用RDBMS。我想念什么?坏了吗
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.selectAll
fun main(args: Array) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
logger.addLogger(StdOutSqlLogger)
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
println("Cities: ${Cities.selectAll()}")
}
}
// Table definition
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey()
val name = varchar("name", 50)
}
// Entity definition
data class City(
val id: Int,
val name: String
)
在Intellij中运行时,我收到以下错误消息:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Table "CITIES" not found; SQL statement:
INSERT INTO CITIES (NAME) VALUES (?) [42102-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5552)
at org.h2.command.Parser.readTableOrView(Parser.java:5529)
at org.h2.command.Parser.parseInsert(Parser.java:1062)
at org.h2.command.Parser.parsePrepared(Parser.java:417)
at org.h2.command.Parser.parse(Parser.java:321)
at org.h2.command.Parser.parse(Parser.java:293)
at org.h2.command.Parser.prepareCommand(Parser.java:258)
at org.h2.engine.Session.prepareLocal(Session.java:578)
at org.h2.engine.Session.prepareCommand(Session.java:519)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1188)
at org.jetbrains.exposed.sql.statements.InsertStatement.prepared(InsertStatement.kt:58)
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:46)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:103)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:97)
at org.jetbrains.exposed.sql.statements.Statement.execute(Statement.kt:27)
at org.jetbrains.exposed.sql.QueriesKt.insert(Queries.kt:43)
at MainKt$main$1.invoke(Main.kt:14)
at MainKt$main$1.invoke(Main.kt)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:92)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:64)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:55)
at MainKt.main(Main.kt:11)
Process finished with exit code 1
Todd..
5
我认为他们的示例中缺少create语句。该项目的GitHub页面上的 DAO示例似乎包含一些您所指向的示例没有的语句。
尝试添加一条create语句:
fun main(args: Array) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
logger.addLogger(StdOutSqlLogger)
// ADD THIS - Create tables
create (Cities)
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
println("Cities: ${Cities.selectAll()}")
}
}
如果可行,我敢打赌他们会接受PR作为其文档。
1> Todd..:
我认为他们的示例中缺少create语句。该项目的GitHub页面上的 DAO示例似乎包含一些您所指向的示例没有的语句。
尝试添加一条create语句:
fun main(args: Array) {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
transaction {
logger.addLogger(StdOutSqlLogger)
// ADD THIS - Create tables
create (Cities)
val stPeteId = Cities.insert {
it[name] = "St. Petersburg"
} get Cities.id
println("Cities: ${Cities.selectAll()}")
}
}
如果可行,我敢打赌他们会接受PR作为其文档。