postgresql - 使用 Chapel CDO 从 Postgres 检索数组

标签 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 "

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

最佳答案

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

例如:

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);

  }

}

关于postgresql - 使用 Chapel CDO 从 Postgres 检索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782174/

相关文章:

chapel - 如何计算 Chapel forall 循环中的迭代次数

derived-class - 在 Chapel 中接受派生类的类方法

ruby-on-rails - 多个 Rails 项目,每个项目都有不同的数据库

sql - 错误 : functions in index expression must be marked IMMUTABLE in Postgres

chapel - 检查/确保两个数组具有相同的域和分布的最佳方法是什么?

sparse-matrix - 稀疏矩阵作为 Chapel 对象中的字段

dictionary - 如何在 Chapel 中表示集合或字典?

postgresql - Amazon EC2 上的数据存储建议,尤其是数据库

hibernate - 在 PostgreSQL 和 SpringBoot (JPA) 中创建序列的问题

database - 从具有 NULL 值的 postgreSQL LEFT JOIN 查询中减去 2 列