tdbc::resultset(n)
π«Ά tdbc::resultset β the TDBC result set object, explained π
okay so bestie, this is a page from Tcl Database Connectivity (that's the tdbc manual, btw β that's the manual name, you're welcome β¨). Sit down, hydrate, and let's talk about the little object that holds all your query rows. π«Ά
β¨ SYNOPSIS β¨
package require tdbc 1.0
package require tdbc::driver version
tdbc::driver::connection create db ?-option value...?
set stmt [db prepare sql-code]
set resultset [$stmt execute ?args...?]
$resultset columns
$resultset rowcount
$resultset nextrow ?-as lists|dicts? ?--? varname
$resultset nextlist varname
$resultset nextdict varname
$resultset nextresults
$resultset allrows ?-as lists|dicts? ?-columnsvariable name? ?--?
$resultset foreach ?-as lists|dicts? ?-columnsvariable name? ?--?
varname script
$resultset close
πΈ DESCRIPTION πΈ
okay so here's the tea β β every single database driver for TDBC (Tcl DataBase Connectivity) implements a result set object, and that object is literally the whole vibe of "here's what came back from running SQL in your database." π You never make one yourself with new or whatever β instances of this object get created when you run the execute object command on a statement object. That's the origin story, periodt.
π The object commands, one by one
columnsβ returns a list of the names of the columns in the result set. And the way that they come back? Same order they appeared in the SQL statement that ran your query. π¦ Slay. Now β important caveat, bestie β if your SQL statement doesn't return a set of columns (think: anINSERT,UPDATE, orDELETE), thencolumnshands you an empty list. Not an error, just β¨nothingβ¨.rowcountβ returns the number of rows in the database that got affected by executing anINSERT,UPDATEorDELETEstatement. And for aSELECTstatement? The row count is unspecified. No cap. Don't rely on it there. πnextlistβ sets the variable you named in varname, in the caller's scope, to the next row of the results, expressed as a list of column values.NULLvalues get replaced by empty strings. The columns of the result row appear in the same order they appeared in theSELECTstatement. The return ofnextlistis1if the operation succeeded, and0if you've hit the end of the result set. πnextdictβ same idea but a different fit: sets the variable given by varname in the caller's scope to the next row of the results, expressed as a dictionary. The dictionary's keys are column names, and the values are the values of those columns in that row. Here's the twist though β if a column's value in the row isNULL, its key is omitted from the dictionary entirely (not set to empty, just... gone, ghosted π). The keys appear in the dictionary in the same order in which the columns appeared on theSELECTstatement. Return is1on success,0at the end of the result set.nextrowβ this one is precisely equivalent to thenextdictornextlistobject command, depending on whether you pass-as dicts(which is the default, iykyk π) or-as lists. That's it, that's the whole thing.nextresultsβ okay so some databases support the idea of a single statement that returns multiple sets of results. Iconic of them, honestly. π§nextresultsis what you run β typically afternextlistornextdicthas returned0β to advance to the next result set. It returns1if there's another result set to process, and0if the result set you just processed was the last one. After callingnextresultsand getting1back, you may once again callcolumnsto get the column descriptions of the next result set, and then go right back to callingnextdictornextlistto chew through the rows of that next result set. π
β οΈ big warning bestie: it is an error to callcolumns,nextdict,nextlistornextrowafternextresultshas returned0. Like, do not. It's giving undefined behaviour energy. π
allrowsβ sets the variable designated by the-columnsvariableoption (if present) to the result of thecolumnsobject command. Then it executes thenextrowobject command repeatedly until the end of the result set is reached. Ifnextresultsreturns a nonzero value, it runs those above two steps again βcolumnsfollowed by iteratednextrowcalls β as long as further results are available. All those rows returned bynextrowget assembled into a Tcl list, and that list becomes the return value of theallrowscommand. And the-columnsvariable? What the application ends up seeing in it is the last value returned fromcolumns. π¦foreachβ sets the variable designated by the-columnsvariableoption (if present) to the result of thecolumnsobject command. Then it executes thenextrowobject command repeatedly until the end of the result set, storing the successive rows in the variable designated by varName. For each row, it executes the given script. Now the script control flow, because this bit matters fr fr:- If the script terminates with an error, the error is reported by the
foreachcommand, and iteration stops. - If the script performs a
breakoperation, the iteration terminates prematurely. - If the script performs a
continueoperation, iteration recommences with the next row. - If the script performs a
return, results are the same as if a script outside the control offoreachhad returned. π - Any other unusual return code terminates the iteration and is reported from the
foreach.
- If the script terminates with an error, the error is reported by the
Once nextrow returns 0, the foreach object command tries to advance to the next result set using nextresults. If nextresults returns 1, the above steps (columns and nextrow, with script invocation) are repeated as long as more result sets remain. And the script will always see the correct description of the columns of the current result set in the variable designated by -columnsvariable. At the end of the call, that variable will hold the description of the columns of the last result set. π
closeβ deletes the result set and frees any associated system resources. Clean up after yourself, bestie, it's self-care. π
π₯ SEE ALSO π₯
encoding(n), tdbc(n), tdbc::connection(n), tdbc::statement(n), tdbc::tokenize(n)
π KEYWORDS π
TDBC, SQL, database, connectivity, connection, resultset, statement, bound variable, stored procedure, call
π COPYRIGHT π
Copyright (c) 2008 by Kevin B. Kenny.
okay that's the whole page, you ate that π«Ά so this one's from the Tcl manual, version 8.6, tdbc::resultset(n) β go forth and iterate responsibly, mwah πβ¨π
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 2008 by Kevin B. Kenny
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms