5

別名NULでpsqlフィールドとレコードを分離する方法はありますか? これは、任意のデータを Bash スクリプト\0に渡すことができる唯一の方法です。

Matthew Woodの回答に基づいて、これ1は新しく初期化されたデータベースでより多く印刷されると予想されます。

declare -i count=0
echo "\pset recordsep '\000'
\f '\000'
select typname from pg_type" | \
sudo -iu postgres psql --no-align --quiet --tuples-only -d dbname -U username | while IFS= read -r -d ''
do
    #echo "$REPLY"
    let count++
done
if [ -n "$REPLY" ]
then
    #echo "$REPLY"
    let count++
fi
echo $count

回避策:SELECT結果が一意である場合は、この回避策を使用して一度に 1 つずつ処理できます。

next_record() {
    psql --no-align --quiet --tuples-only -d dbname -U username <<SQL
SELECT colname
  FROM tablename
 WHERE colname > '${1}'
 ORDER BY colname
 LIMIT 1
SQL
}

last_col=
while true
do
    colx="$(next_record "$last_col"; printf x)"
    if [ "$colx" = x ]
    then
        exit
    fi
    col="${colx%$'\nx'}" # The extra \n character is from psql

    # Do your thing here

    col_escaped="${col//"'"/''}" # Double single quotes
    col_escaped="${col_escaped//\\/\\\\}" # Double backslashes
    last_col="$col_escaped"
done
4

3 に答える 3

4

これはサポートされていません。psql は C 印刷関数を使用して結果テーブルを出力しますが、ゼロ バイトの出力は機能しません。

更新: これは PostgreSQL 9.2-to-be ( git ) でサポートされるようになりました。

于 2011-07-30T06:37:40.253 に答える
1

これを試して:

psql --field-separator '\000' --no-align -c '<your query>'

編集:そうではないかもしれません。ただし、次のコマンドを使用して psql で動作するようです。

\f '\000'
\a
于 2011-07-29T00:08:17.967 に答える