Tuang
02/05/2020, 2:50 PMval query = "INSERT INTO table (id, name, age) VALUES (?,?,?)"
this is ordinary way (i usually use) and the problem is, the parameter `?`need to append according to the increase or decrease of column id, name, age
so i tried,,,
var column = "id, name, age"
val query = "INSERT INTO table (${column}) VALUES (${(1..3).forEach { ? }})"
the part ${(1..3).forEach { ? }}
is incorrect, i don’t know how to do.
hope you got me,Alowaniak
02/05/2020, 2:55 PMval columns = listOf("id", "name", "age")
println("INSERT INTO table (${columns.joinToString(", ")}) VALUES (${columns.map{ "?" }.joinToString(", ")})")
I think you're better off using some framework to create typesafe queries etc. thoughRuckus
02/05/2020, 3:07 PMcolumns.joinToString(", ") { "?" }
Matteo Mirk
02/05/2020, 3:48 PMJames Richardson
02/05/2020, 3:57 PMMatteo Mirk
02/05/2020, 4:03 PMBrian Dilley
02/05/2020, 4:38 PMfun <T> Iterable<T>.joinToSQLIn(separator: CharSequence = ",", prefix: CharSequence = "(", postfix: CharSequence = ")", limit: Int = -1, transform: ((T) -> CharSequence)? = { _ -> "?"}): String
= this.joinToString(separator = separator, prefix = prefix, postfix = postfix, limit = limit, transform = transform)
then:
SELECT ${cols.joinToSQLIn()} ...
Tuang
02/05/2020, 11:36 PMDico
02/06/2020, 9:38 AMBrian Dilley
02/06/2020, 4:54 PM