5.13.Example - COPY FROM and COPY TO
Prev | Chapter5.Example Programs |

## 5.13.Example - COPY FROM and COPY TO

これ等の例では､データベースの表へ､又は､表から､塊の転送を遂行する為に如何に SQL COPY を使うかを示す｡更なるCOPYの情報は､PostgreSQL参照取扱説明書を参照されたい｡ These examples show how to use SQL COPY to perform bulk transfers to and from a database table. For more information on COPY, refer to the PostgreSQL Reference Manual.

### 註意 Note

pgintcl は､ COPY の間の読み書きに特別の命令を使うので､この用例は動かない｡ 詳細は pgintcl の取扱説明書を参照されたい｡ This example does not work with pgintcl, which uses special commands to read and write during COPY. Refer to the pgintcl documentation for details.

最初の例では､データを表に複写する｡ SQL COPY 命令を送った後､ タブ分離データがデータベース接続把持子に､行毎に一つの記録が､直接書込まれる｡ データの末尾は､Tclで "\\."と書かれる様な､逆斜線に点を続けて送って知らせる｡ これは､接続を通常様式に戻し､複写の最終状態を確認できる｡(訳註 訳が怪しい) The first example copies data into a table. After sending the SQL COPY command, tab-separated data is written directly to the database connection handle, one record per line. End of data is signaled by sending a line with a backslash followed by a dot, written in Tcl as "\\.". This converts the connection back to its normal mode, and the final status of the copy can be verified.

Example5.20.COPY TO

      # SQL COPY 命令を送った後､最初の状態を確認する:
      # Send the SQL COPY command and check initial status:
      set res [pg_exec $conn "COPY mytable FROM STDIN"]
      if {[pg_result $res -status] != "PGRES_COPY_IN"} {
        set message [pg_result $res -error]
        pg_result $res -clear
        error $message
      }

      # データベースに書込む:
      # Write to the database:
      for {set i 0} {$i < 100} {incr i} {
        puts $conn "$i\tThis is data line $i"
      }

      # データ末尾印を書込む:
      # Write the end of data marker:
      puts $conn "\\."

      # 最終状態を確認:
      # Check final status:
      if {[pg_result $res -status] != "PGRES_COMMAND_OK"} {
        set message [pg_result $res -error]
        pg_result $res -clear
        error $message
      }
      pg_result $res -clear

第二用例は､表からデータを読む｡ SQL COPY命令を送った後､データベース接続把持子から､ 一行毎に一つの記録の､表に-区切られた データが､直接読まれる｡ ｔｃｌがデータベース接続把持子通信路からファイル末尾が見ると､複写は完了し､ 接続はその正常様式に戻り､ 複写の最終状態が確認できる｡(訳註 訳が怪しい) The second example copies data from a table. After sending the SQL COPY command, tab-separated data is read directly from the database connection handle, one record per line. When Tcl sees End of File on the database connection handle channel, the copy is complete, the connection is back in its normal mode, and the final status of the copy can be verified.

Example5.21.COPY FROM

      # SQL COPY 命令を送った後､最初の状態を確認する:
      # Send the SQL COPY command and check initial status:
      set res [pg_exec $conn "COPY mytable TO STDOUT"]
      if {[pg_result $res -status] != "PGRES_COPY_OUT"} {
        set message [pg_result $res -error]
        pg_result $res -clear
        error $message
      }

      # EOF 迄読出す
      # Read until EOF
      while {[gets $conn line] >= 0} {
        puts "Read line: $line"
      }

      # 最終状態を確認す:
      # Check final status:
      if {[pg_result $res -status] != "PGRES_COMMAND_OK"} {
        set message [pg_result $res -error]
        pg_result $res -clear
        error $message
      }
      pg_result $res -clear

Prev | Up |
5.12.Example - Asynchronous Query with Event-Driven Results | Home |