通过在PostgreSQL中拆分字符串将值插入多个列

时间:2013-10-17 05:04:29

标签: postgresql

我有以下文本堆:

"BundleSize,155648,DynamicSize,204800,Identifier,com.URLConnectionSample,Name,
URLConnectionSample,ShortVersion,1.0,Version,1.0,BundleSize,155648,DynamicSize,
16384,Identifier,com.IdentifierForVendor3,Name,IdentifierForVendor3,ShortVersion,
1.0,Version,1.0,".

我想做的是通过以下方式从中提取数据:

BundleSize:155648
DynamicSize:204800
Identifier:com.URLConnectionSample
Name:URLConnectionSample
ShortVersion:1.0
Version:1.0

BundleSize:155648
DynamicSize:16384
Identifier:com.IdentifierForVendor3 
Name:IdentifierForVendor3
ShortVersion:1.0
Version:1.0

欢迎提供所有提示和建议。

1 个答案:

答案 0 :(得分:1)

目前尚不清楚您需要对此数据做什么。如果你真的需要在数据库中完全处理它(看起来像你最喜欢的脚本语言的任务),一个选项是使用hstore

逐个转换记录很简单:

假设

  

%s =   BundleSize,155648,DynamicSize,204800,标识符,com.URLConnectionSample,名称,URLConnectionSample,ShortVersion,1.0,版本,1.0

SELECT * FROM each(hstore(string_to_array(%s, ',')));

输出:

     key      |          value          
--------------+-------------------------
 Name         | URLConnectionSample
 Version      | 1.0
 BundleSize   | 155648
 Identifier   | com.URLConnectionSample
 DynamicSize  | 204800
 ShortVersion | 1.0

如果您的表中的列与字段名称完全匹配(请注意引号,populate_record对键名称区分大小写):

CREATE TABLE data (
    "BundleSize" integer, "DynamicSize" integer, "Identifier" text,
    "Name" text, "ShortVersion" text, "Version" text);

您可以像这样插入hstore条记录:

INSERT INTO data SELECT * FROM
    populate_record(NULL::data, hstore(string_to_array(%s, ',')));

如果您有多个记录的逗号分隔值,事情会变得更加复杂。

  

%s = BundleSize,155648,DynamicSize,204800,Identifier,com.URLConnectionSample,Name,URLConnectionSample,ShortVersion,1.0,Version,1.0,BundleSize,155648,DynamicSize,16384,Identifier,com.IdentifierForVendor3,Name,IdentifierForVendor3, ShortVersion,1.0,版本,1.0,

您需要先将数组拆分为number_of_fields * 2 = 12个元素块。

SELECT hstore(row) FROM (
    SELECT array_agg(str) AS row FROM (
        SELECT str, row_number() OVER () AS i FROM
        unnest(string_to_array(%s, ',')) AS str
    ) AS str_sub
    GROUP BY (i - 1) / 12) AS row_sub
WHERE array_length(row, 1) = 12;

输出:

 "Name"=>"URLConnectionSample", "Version"=>"1.0", "BundleSize"=>"155648", "Identifier"=>"com.URLConnectionSample", "DynamicSize"=>"204800", "ShortVersion"=>"1.0"
 "Name"=>"IdentifierForVendor3", "Version"=>"1.0", "BundleSize"=>"155648", "Identifier"=>"com.IdentifierForVendor3", "DynamicSize"=>"16384", "ShortVersion"=>"1.0"

将其插入上述表格中:

INSERT INTO data SELECT (populate_record(NULL::data, hstore(row))).* FROM ...

查询的其余部分是相同的。