如何使用参数执行 sql 查询?
How can i execute the sql query with parameter?
val resultList: List[List[String]] =
sql"""
select * from exchange_transaction
"""
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
执行上面的代码,就可以了
但是这个 sql "select * from exchange_transaction" 不是固定的。
我想要这个 sql 像一个参数,就像下面的(shorthand 代码.....)
def findMemberList(segmentExecuteSql: String
val resultList: List[List[String]] =
sql"""
$segmentExecuteSql
"""
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
但是执行后报错
消息是
[error] s.StatementExecutor$$anon - SQL execution failed (Reason: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from exchange_transaction'' at line 1
Query is :
'select * from exchange_transaction'
):
'select * from exchange_transaction'
我该怎么办,如何解决异常?
你可以试试把resultList
变成一个函数:
import scalikejdbc.SQL
def resultList(sqlQuery: String): List[List[String]] =
SQL(sqlQuery)
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
val resultList: List[List[String]] =
SQL(segmentExecuteSql)
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
val resultList: List[List[String]] =
sql"""
select * from exchange_transaction
"""
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
执行上面的代码,就可以了 但是这个 sql "select * from exchange_transaction" 不是固定的。 我想要这个 sql 像一个参数,就像下面的(shorthand 代码.....)
def findMemberList(segmentExecuteSql: String
val resultList: List[List[String]] =
sql"""
$segmentExecuteSql
"""
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
但是执行后报错 消息是
[error] s.StatementExecutor$$anon - SQL execution failed (Reason: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from exchange_transaction'' at line 1
Query is :
'select * from exchange_transaction'
):
'select * from exchange_transaction'
我该怎么办,如何解决异常?
你可以试试把resultList
变成一个函数:
import scalikejdbc.SQL
def resultList(sqlQuery: String): List[List[String]] =
SQL(sqlQuery)
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()
val resultList: List[List[String]] =
SQL(segmentExecuteSql)
.map(
rs =>
List(
rs.string("transaction_id")
)
)
.list()
.apply()