[PostgreSQL] and [Tcl] combine quite pleasantly. It can be a delight using them for substantial development projects. Much the more demanding, at least in setup, is the former. Here's what it takes to start: ... [[to be finished]] ---- Ugh. I'm left hanging in anticipation... Seriously, this is a problem I have right now. If the original author (or anybody else) wants to add to this page, it would be *greatly* appreciated. ---- '''1. Setup PostgreSQL''' Either just install your OS specific package if it is available for example the postgreSQL rpm, or start reading the fine postgreSQL install instructions at: http://www.postgresql.org/docs/7.4/interactive/installation.html Make sure you have a running server, get its connection info like hostname, port, user and password for the user you want to use for the next steps. '''2. Make sure you have a working Pgtcl package''' Try: package require Pgtcl If it works, you have a probably working Pgtcl package installed, otherwise you need to install/compile one from either the PostgreSQL distribution you have installed (Pgtcl should be included there) or obtain and build [libpgtcl]. '''3. Start using Pgtcl''' Take a look at: http://www.postgresql.org/docs/7.4/interactive/pgtcl.html The basics: # select and do something with the results... # open connection # password etc. must be provided according to the security settings on the postgres db # you may be able to just do "pg_connect www" or even "pg_connect" if database is your username package require Pgtcl set db [pg_connect -conninfo [list host = localhost user = test dbname = testdb] pg_select $conn "select * from testtable" user { parray user } pg_disconnect $db Inserting into a database... Note how pg_exec returns a result handle that has many things that can be done with it to access the results. You can get a list interactively by doing a pg_result with no arguments. You gotta clean up the result handle this way. Note that pg_select, above, takes care of all that for you. package require Pgtcl proc doit {} { set fp [open sampledata.txt] set conn [pg_connect -conninfo ""] while {[gets $fp line] >= 0} { lassign $line name address city state zip set statement "insert into peopletable values ( \ [pg_quote $name], [pg_quote $address], [pg_quote $city] \ [pg_quote $state], [pg_quote $zip]);" set result [pg_exec $conn $statement] if {[pg_result $result -status] != "PGRES_COMMAND_OK"} { puts "[pg_result $result -error] executing '$statement'" } pg_result $result -clear } } if !$tcl_interactive doit Recent version of Pgtcl running with fairly recent version of PostgreSQL can do variable substitutions, which are pretty cool, and require less quoting and stuff. Observe... set statement "insert into peopletable values ($1, $2, $3, $4, $5);" set result [pg_exec $conn $statement $name $address $city $state $zip] '''4. Pgtcl Commands''' pg_quote This escapes a string by making it Postgres-safe. It quotes single quotes and backslashes. If you're doing something like pg_exec "insert into foo(name='$name');" and name contains a single quote, it'll fail. Passign value strings through pg_quote make sure they can be used as values and stuff in Postgres. pg_exec "insert into foo(name=[pg_quote $name]);" will make sure that any special characters that occur in name, such as single quote or backslash, will be properly quoted. pg_connect -conninfo conninfoString The modern way to connect, all necessary information required for the connection (host, port, database, user, password, etc.) is specified in the connect info string. pg_disconnect connection Close a backend connection. pg_exec connection query Send a query string to the backend connection. The return result is either an error message or a handle for a query result. Handles start with the prefix "pgp". pg_select Send a select query string to the backend connection. pg_select connection query arrayName code The query must be a select statement. The code is run once for each row found. See the example earlier in this page. You can "continue" and "return" in the body and it'll do the expected thing. (What I think is expected, anyway.) pg_result Get information about the results of a query. pg_result result ?option? The options are: -status the status of the result -error the error message, if the status indicates error; otherwise an empty string -conn the connection that produced the result -oid if command was an INSERT, the OID of the inserted tuple -numTuples the number of tuples in the query -numAttrs returns the number of attributes returned by the query -assign arrayName assign the results to an array, using subscripts of the form (tupno,attributeName) -assignbyidx arrayName ?appendstr? assign the results to an array using the first field's value as a key. All but the first field of each tuple are stored, using subscripts of the form (field0value,attributeNameappendstr) -getTuple tupleNumber returns the values of the tuple in a list -tupleArray tupleNumber arrayName stores the values of the tuple in array arrayName, indexed by the attributes returned -attributes returns a list of the name/type pairs of the tuple attributes -lAttributes returns a list of the {name type len} entries of the tuple attributes -clear clear the result buffer. Do not reuse after this