All pastes #469163 Raw Edit

Vadtec

public text v1 · immutable
·published 2007-05-03 16:15 UTC
rendered paste body
proc find_next { db db_name t col } {
	# This proc finds the next numerical value for a given column.
	# This is most useful with an "ID" column, such as that in a 
	# users table where you want to reuse user IDs.
	
	# db:      The database file name
	# db_name: The name of the database in case there is more than one in the database file. Can be "".
	# t:       The table name
	# col:     The column to process
	
	set val ""
		
	sqlite3 dbs $db

	if {$db_name ne ""} {
		set sql "SELECT $col FROM $db_name.$t"

		putlog ">>> 1 >> 1 > sql: $sql"
		
		if {[catch { [set val [dbs eval {$sql}]] }]} {
			foreach l [split $::errorInfo \n] {
				putlog "ERROR:trace - $l"
			}
		} else {
			putlog ">>> 1 >> 3"
		}
	} else {
		set sql "SELECT $col FROM $t"

		putlog ">>> 1 >> 2 > sql: $sql"
		
		if {[catch { [set val [dbs eval {$sql}]] }]} {
			foreach l [split $::errorInfo \n] {
				putlog "ERROR:trace - $l"
			}
		} else {
			putlog ">>> 1 >> 4"
		}
	}
	
	dbs close
		
	return $val
}

When I run the following:
set id [find_next "test.db" "main" "domain" "id"]

I get:
[22:14] >>> 1 >> 1 > sql: SELECT id FROM main.domain
[22:14] ERROR:trace - near "$sql": syntax error
[22:14] ERROR:trace -     while executing
[22:14] ERROR:trace - "dbs eval {$sql}"


When I run the following:
set id [find_next "test.db" "" "test" "id"]

I get:
[22:15] >>> 1 >> 2 > sql: SELECT id FROM domain
[22:15] ERROR:trace - near "$sql": syntax error
[22:15] ERROR:trace -     while executing
[22:15] ERROR:trace - "dbs eval {$sql}"