使用Chapel CDO从Postgres检索数组

时间:2017-12-12 21:55:07

标签: postgresql chapel

我在使用Chapel CDO Library从Postgres中提取数组时遇到错误我有一个带有下表的pg实例

DROP TABLE IF EXISTS aagg;
CREATE TABLE aagg (team text, name text);
INSERT INTO aagg VALUES ('Wonder Pets', 'Linny'); 
INSERT INTO aagg VALUES ('Wonder Pets', 'Ming Ming');
INSERT INTO aagg VALUES ('Wonder Pets', 'Tuck');
INSERT INTO aagg VALUES ('OJ Defense Team', 'F. Lee Bailey'); 
INSERT INTO aagg VALUES ('OJ Defense Team', 'Robert Shapiro');
INSERT INTO aagg VALUES ('OJ Defense Team', 'Johnny Cohchran');

我试图通过以下教堂计划来拉动它

use Postgres;

config const DB_HOST: string = "localhost";
config const DB_USER: string = "buddha";
config const DB_NAME: string = "buddha";
config const DB_PWD: string = "buddha";


var con = PgConnectionFactory(host=DB_HOST, user=DB_USER, database=DB_NAME, passwd=DB_PWD);
var cursor = con.cursor();
// Retrieve the data
const q = "SELECT team, array_agg(name) AS members FROM aagg GROUP BY team;"; 
cursor.query(q);

for row in cursor {
  writeln("Team: ", row['name'], "\tMembers: ", row['members'] );
  for member in row['members'] {
    writeln ("Special mention to ", member);
  }
}

但循环分解了

中的字符
Special mention to {
Special mention to "
Special mention to F
Special mention to .
Special mention to  
Special mention to L
Special mention to e
Special mention to e
Special mention to  
Special mention to B
Special mention to a
Special mention to i
Special mention to l
Special mention to e
Special mention to y
Special mention to "

如何识别阵列?谢谢!

1 个答案:

答案 0 :(得分:2)

使用download_urlrow["column_name"]时,您将获取列值作为字符串。但是,Postgres可以使用列数组响应查询。要对此进行处理,您应该使用 row.get("column_name") 方法将Postgres数组作为Chapel字符串数组。

例:

row.getArray("column_name")