我有一个内容文件:
( [datname]=template1 [datctype]=cs_CZ.utf-8 )
( [datname]=template0 [datctype]=cs_CZ.utf-8 )
( [datname]=postgres [datctype]=cs_CZ.utf-8 )
( [datname]=some\ stupid\ name [datctype]=cs_CZ.utf-8 )
( [datname]=jqerqwer,\ werwer [datctype]=cs_CZ.utf-8 )
Run Code Online (Sandbox Code Playgroud)
我想读取每一行并将上下文推送到关联数组变量.以下代码我没有成功:
(cat <<EOF
( [datname]=template1 [datctype]=cs_CZ.utf-8)
( [datname]=template0 [datctype]=cs_CZ.utf-8 )
EOF
) |
while read r
do
declare -A row=("$r")
echo ${row[datname]}
done;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
test3.sh: line 8: row: ( [datname]=template1 [datctype]=cs_CZ.utf-8 ): must use subscript when assigning associative array
Run Code Online (Sandbox Code Playgroud)
可以从文件中读取数组吗?
我正在写寻呼机pspg.我必须解决以下问题.读完之后,stdin我应该stdin从之前的阅读管道重新分配到终端阅读.
我用了
freopen("/dev/tty", "r", stdin)
Run Code Online (Sandbox Code Playgroud)
但是,当从命令中使用寻呼机时,它不起作用
su - someuser -c 'export PAGER=pspg psql somedb'
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我收到一个错误:没有这样的设备或地址.
我找到了一个解决方法 - 现在,代码看起来像:
if (freopen("/dev/tty", "r", stdin) == NULL)
{
/*
* try to reopen pty.
* Workaround from:
* https://cboard.cprogramming.com/c-programming/172533-how-read-pipe-while-keeping-interactive-keyboard-c.html
*/
if (freopen(ttyname(fileno(stdout)), "r", stdin) == NULL)
{
fprintf(stderr, "cannot to reopen stdin: %s\n", strerror(errno));
exit(1);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,检测分配的终端设备的正确方法是什么?
但这种解决方法不正确.它解决了一个问题,但接下来就要来了.当某个用户与当前用户不同时,重新打开失败,并显示" 权限被拒绝"错误.因此,此解决方法不能用于我的目的.
我有一个变量,它是一个表的名称.如何在查询中使用变量选择或更新此变量,例如:
create or replace function pg_temp.testtst ()
returns varchar(255) as
$$
declare
r record; t_name name;
begin
for r in SELECT tablename FROM pg_tables WHERE schemaname = 'public' limit 100 loop
t_name = r.tablename;
update t_name set id = 10 where id = 15;
end loop;
return seq_name;
end;
$$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
它显示 ERROR:关系"t_name"不存在
在 plpgsql 函数中,我有一个简单的插入语句:
insert into table_Y (column1 ,
column2 ,
//more columns
int_date , -- example: '20190714'
//more columns
)
select value1 ,
value2 ,
//more values
date_value::integer ,
//more values
from table_X
Run Code Online (Sandbox Code Playgroud)
表 table_X 中的列“date_value”是日期类型。表 table_Y 中的列 int_date 是整数类型......所以使用以下表达式:
date_value::integer
Run Code Online (Sandbox Code Playgroud)
...我想将“date_value”列中的值转换为整数(例如,从 2019 年 7 月 14 日到“20190714”)。
但是,我收到以下错误消息:
ERROR: cannot cast type date to integer
Run Code Online (Sandbox Code Playgroud)
如何将日期转换为整数?
PS:这个问题的解决方案:在postgresql中将日期转换为整数在我的情况下没有帮助,因为我不想要天数。
编辑
看来我的问题是当这个select语句返回null时(我正在尝试处理的情况 - 当它返回null时,我希望我的新值为-999).如果在找到null时出错,我怎么能这样做呢?
原版的
我已经阅读了有关此错误的所有其他SO帖子,但其中没有一个似乎解决了我的问题的根源.
错误非常简单 - 我的EXECUTE语句中的一个参数为null.大.但是,我在调用之前打印出构成我的EXECUTE语句的每个值,并且我可以清楚地看到没有值为null.
码:
CREATE FUNCTION inform_icrm_prob_flow_query(tablename text, location_id int,
product_date_str text, lead_time_start int,
lead_time_end int, first_member_id int,
last_member_id int, dest_file text)
RETURNS void AS $$
DECLARE
count int;
product_date TIMESTAMPTZ;
interval_lead_time_start text;
interval_lead_time_end text;
curr_value double precision;
query text;
BEGIN
product_date := product_date_str::TIMESTAMPTZ;
count := first_member_id;
curr_value := 0;
interval_lead_time_start := ''''|| product_date ||'''::timestamptz +
interval '''||lead_time_start||' hours''';
interval_lead_time_end := ''''|| product_date ||'''::timestamptz +
interval '''||lead_time_end||' hours'' -
interval ''6 hours''';
--create our …Run Code Online (Sandbox Code Playgroud) 我想创建一个 postgresql c 语言函数,其参数和返回值是 varchar,并且在处理过程中使用 Char []。
但是,它并没有按预期工作。
我做了一个这样的c程序。
#include <postgres.h>
#include <port.h>
#include <fmgr.h>
#include <stdlib.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
// Required for windows.
extern PGDLLEXPORT Datum VARCHAR_CHAR_ARRAY_VARCHAR(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(VARCHAR_CHAR_ARRAY_VARCHAR);
Datum VARCHAR_CHAR_ARRAY_VARCHAR(PG_FUNCTION_ARGS) {
// Get arg.
VarChar *arg1 = (VarChar *)PG_GETARG_VARCHAR_P(0);
// VarChar to Char[].
char *c = (char *)VARDATA(arg1);
elog(NOTICE, "VarChar to Char[].");
elog(NOTICE, c);//Log1
// Do something.(e.g. replace)
// Since it uses another system, it must be Char [].
//Char[] to VarChar.
VarChar *rtn = (VarChar *)VARDATA(c);
elog(NOTICE, …Run Code Online (Sandbox Code Playgroud)