使用 Chapel CDO 从 Postgres 中检索数组
Retrieve an array from Postgres using Chapel CDO
我在使用 Chapel CDO Library 从 Postgres 中拉取数组时出错 我有一个带有以下内容的 pg 实例 table
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);
}
}
我在使用 Chapel CDO Library 从 Postgres 中拉取数组时出错 我有一个带有以下内容的 pg 实例 table
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);
}
}