Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在迭代过程中,如何将psql中的记录保存到文件中_Excel_Postgresql_Csv_Ssh_Psql - Fatal编程技术网

Excel 在迭代过程中,如何将psql中的记录保存到文件中

Excel 在迭代过程中,如何将psql中的记录保存到文件中,excel,postgresql,csv,ssh,psql,Excel,Postgresql,Csv,Ssh,Psql,我有一个只包含id的文件。id与表中的一列匹配。我想提取该列中的记录,并在csv文件中获得结果。我不想再创建一个表 我有docid.txt文件,其中包含以下数据: 12000072 112073 11279 1182 11188 11182094 82099 11102 每一个都对应于学生表中的nameid列。学生表有: name nameid dob performance -------------------------------------------

我有一个只包含id的文件。id与表中的一列匹配。我想提取该列中的记录,并在csv文件中获得结果。我不想再创建一个表

我有docid.txt文件,其中包含以下数据:

12000072
112073
11279
1182
11188
11182094
82099
11102
每一个都对应于学生表中的nameid列。学生表有:

name    nameid      dob         performance
-------------------------------------------
andre   12000072    12/4/1990   Excellent
Scott   112073      12/5/1999   Good
Reagan  2692882     15/7/2003   fair
Aldo    1402508     4/7/1998    Poor
Mary    2266257     5/11/1995   Very good
Walt    11182094    3/12/2000   Good
Tessy   82099       8/12/1999   Very good
Skinn   11102       7/2/2002    Excellent
Smart   678016      12/4/1990   fair
John    475689      12/5/1999   Poor
Rita    2796799    12/4/1990    Very good
Rachael 11188      12/5/1999    Poor
Gomez   3075168    3/12/2000    Very good
Becham  3075168    4/7/1998     Good
Walker  1050879    5/11/1995    Very good
Tommy   2017451    3/12/2000    Excellent
Trevor  11279      7/2/2002     Good
Litin   1182       12/5/1999    fair
Martha  883368     15/7/2003    fair
Rafael  3070833    4/7/1998     Poor
Kim     3070833    5/11/1995    Very good
Clichy  255918     12/4/1990    Good
Mario   2706915    12/5/1999    Excellent
我想从学生表中删除具有docid的学生。我尝试了以下方法:

for i in `cat docid.txt;` do
`psql -A -t $dbname $username << EOF`
`select * from student where nameid = $i;`
`EOF`
`done  >>docid.csv`

谢谢

假设您正在使用PostgreSQL,我建议将它们复制到临时表中,然后加入它

e、 g

除了比循环更干净、更简单之外,它还将更快,并且作为一个单元,它将成功或失败,而不会在错误上留下完成一半的工作


如果要输出已删除的行,请将RETURNING*附加到DELETE语句中,并使用psql-q运行以抑制其他输出。

您使用的是什么数据库引擎?在linux上,您的标记混淆了.postgre。第一个文档可以从任何csv文件Excel、csv甚至txt中读取,但该表来自postgre。我没有在数据库中创建表的权限。我想到了这一点,但我在服务器上远程工作,我没有在其中创建表的权限。我需要一个不创建表的解决方案。谢谢,即使不能创建普通表,您也应该能够创建临时表。如果没有,请向管理员询问您用户的临时权限。如果你真的,真的不能得到它,你可以用脚本把你的数据变成一个大的VALUES子句并加入其中。否则,您必须编写一个脚本,执行开始、读取输入文件并将每一行作为删除发送,然后执行提交。Python和psycopg2可以很好地实现这一点。
$ psql <<'__END__'
BEGIN;
CREATE TEMPORARY TABLE docids (docid integer) ON COMMIT DROP;
\copy docids from 'docids.txt'
DELETE FROM student USING docids WHERE nameid = docid;
COMMIT;
__END__