最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - How to perform a multi row insert in postgres with a prepared statement - Stack Overflow

programmeradmin3浏览0评论

I'm trying to use the multi row insert with postgres

INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);

on a list of Strings that can of course be of variable length.

How can I use this with a prepared statement?

I'm trying to use the multi row insert with postgres

INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);

on a list of Strings that can of course be of variable length.

How can I use this with a prepared statement?

Share Improve this question asked 2 days ago Somaiah KumberaSomaiah Kumbera 7,5394 gold badges46 silver badges48 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

The answer is with Postgres unnest. This is postgres proprietary so it unfortunately won't work with all SQL:

val ssnList: List<String> = listOf("1", "2")

conn.prepareStatement("insert into ssns values (unnest(?))").use { stm ->
    val array = conn.createArrayOf("VARCHAR", ssnList.toTypedArray())
    stm.setArray(1, array)
    stm.execute()
}
发布评论

评论列表(0)

  1. 暂无评论