Python列表到PostgreSQL数组 [英] Python List to PostgreSQL Array
本文介绍了Python列表到PostgreSQL数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表:
[u'ABC', u'DEF', u'GHI']
我必须将其插入到postgresql数组中:
( ALTER TABLE aTable添加列 Test文本[];
)
I have to insert it into a postgresql array:
(ALTER TABLE "aTable" ADD COLUMN "Test" text[];
)
将数据添加到postgresql的语法为:
The syntax for adding data to the postgresql is:
update "aTable" SET "Test" = '{"ABC", "DEF", "GHI"}'
如何将列表转换为正确的格式?
How can I convert the list to the correct format?
推荐答案
list = [u'ABC', u'DEF', u'GHI']
list = str(map(str, list))
list = list.replace('[', '{').replace(']', '}').replace('\'', '\"')
query = '''update "aTable" SET "Test" = '%s\'''' %(list)
print query
将导致
update "aTable" SET "Test" = '{"ABC", "DEF", "GHI"}'
这篇关于Python列表到PostgreSQL数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文