我是Slick的新手。我想知道如何使用Slick在scala中生成通用SQL查询 这是我在scala中使用jdbc进行泛型插入操作的示例代码, 想要使用同样的光滑:
def insert(connection: Connection,
tableName: String,
columnList: List[String],
valueList: List[Any]): Option[Int] = {
try {
val query = queryBuilderInsert(tableName, columnList)
val pst = connection.prepareStatement(query)
if (valueList.nonEmpty) {
for { i <- 1 to valueList.size } {
pst.setObject(i, valueList(i - 1))
}
}
Some(pst.executeUpdate())
} catch {
case e: Exception =>
// Exception Handling
None
}
}
def queryBuilderInsert(tableName: String,columnList: List[String]): String= {
val paramList = new ListBuffer[String]
val query = new StringBuilder("insert into " + tableName + "(")
if (columnList.nonEmpty) {
for { i <- 1 to columnList.size } {
query.append(columnList(i - 1))
if (i != columnList.size) {
query.append(comma)
}
else {
query.append(") values(")
}
}
}
if (columnList.nonEmpty) {
for { i <- 1 to columnList.size } {
paramList.append(questionMark)
}
query.append(paramList.mkString(comma) + ")")
}
query.toString()
}