Reputation: 9859
I need to convert List
to quoted string. My code is:
void main() {
List<String> list = ["aa", "bb", "cc"];
String str = "INSERT INTO (" + list.toString() + ") VALUES (...) ";
print(str);
}
output:
INSERT INTO ([aa, bb, cc]) VALUES (...)
But I need:
INSERT INTO ("aa", "bb", "cc") VALUES (...)
Upvotes: 0
Views: 44
Reputation: 31229
void main() {
List<String> list = ["aa", "bb", "cc"];
String str =
"INSERT INTO (" + list.map((e) => '"$e"').join(', ') + ") VALUES (...) ";
print(str); // INSERT INTO ("aa", "bb", "cc") VALUES (...)
}
Can also be done like this which are a little shorter:
void main() {
List<String> list = ["aa", "bb", "cc"];
String str =
"INSERT INTO (${list.map((e) => '"$e"').join(', ')}) VALUES (...) ";
print(str); // INSERT INTO ("aa", "bb", "cc") VALUES (...)
}
I should add that this is just a solution for solving your specific problem. For real SQL escaping from unknown input, you should take into account that the String can contain "
and need to escape it correctly.
Upvotes: 3