File: import_sampledata_prepared.tcl

package info (click to toggle)
pgtcl 1%3A1.9%2Bgit20120911-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,240 kB
  • ctags: 315
  • sloc: ansic: 4,475; tcl: 634; sh: 149; makefile: 36; sql: 11
file content (41 lines) | stat: -rw-r--r-- 1,101 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#
# program to insert the sampledata.txt data set into pgtest_people
#  using a prepared statement.
#
# $Id$
#

package require Pgtcl

if {[file exists conninfo.tcl]} {
    source conninfo.tcl
}

proc doit {} {
    set fp [open sampledata.txt]

    if {[info exists ::conninfo]} {
        set conn [pg_connect -connlist [array get ::conninfo]]
    } else {
        set conn [pg_connect -conninfo ""]
    }

    set result [pg_exec $conn {prepare pgtest_insert_people (varchar, varchar, varchar, varchar, varchar, varchar) as insert into pgtest_people values ($1, $2, $3, $4, $5, $6);}]
    if {[pg_result $result -status] != "PGRES_COMMAND_OK"} {
	puts "[pg_result $result -error] preparing statement"
	exit 1
    }

    while {[gets $fp line] >= 0} {
	set result [pg_exec_prepared $conn pgtest_insert_people [lindex $line 0] [lindex $line 1] [lindex $line 2] [lindex $line 3] [lindex $line 4] [lindex $line 5]]
        if {[pg_result $result -status] != "PGRES_COMMAND_OK"} {
	    puts "[pg_result $result -error] inserting '$line'"
	}
	pg_result $result -clear
    }
}

if !$tcl_interactive doit