首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Chapel CDO从Postgres检索数组

使用Chapel CDO从Postgres检索数组
EN

Stack Overflow用户
提问于 2017-12-12 21:55:06
回答 1查看 80关注 0票数 2

使用礼拜堂图书馆从Postgres提取数组时出错,我有一个pg实例和下表

代码语言:javascript
复制
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');

我正试着用下面的小礼拜堂计划

代码语言:javascript
复制
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);
  }
}

但是循环分解字符,如

代码语言:javascript
复制
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 "

我如何让它识别数组?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-14 15:29:43

使用row["column_name"]row.get("column_name")时,将列值作为字符串获得。但是,Postgres可以使用列数组响应查询。要处理这个问题,您应该使用row.getArray("column_name")方法将Postgres数组作为Chapel字符串数组。

前:

代码语言:javascript
复制
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.getArray("members") {

    writeln ("Special mention to ", member);

  }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47782174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档