Need max values in list of tuples, based on position

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Nov 11 19:04:25 EST 2022


On Fri, 11 Nov 2022 15:03:49 -0500, DFS <nospam at dfs.com> declaimed the
following:


>Thanks for looking at it.  I'm trying to determine the maximum length of 
>each column result in a SQL query.  Normally you can use the 3rd value 
>of the cursor.description object (see the DB-API spec), but apparently 
>not with my dbms (SQLite).  The 'display_size' column is None with 
>SQLite.  So I had to resort to another way.

	Not really a surprise. SQLite doesn't really have column widths --
since any column can store data of any type; affinities just drive it into
what may be the optimal storage for the column... That is, if a column is
"INT", SQLite will attempt to convert whatever the data is into an integer
-- but if the data is not representable as an integer, it will be stored as
the next best form.

	123		=> stored as integer
	"123"	=> converted and stored as integer
	123.0	=> probably converted to integer
	123.5	=> likely stored as numeric/double
	"one two three"	=> can't convert, store it as a string

	We've not seen the SQL query in question, but it might suffice to use a
second (first?) SQL query with aggregate (untested)

		max(length(colname))

for each column in the main SQL query.

"""
length(X)

    For a string value X, the length(X) function returns the number of
characters (not bytes) in X prior to the first NUL character. Since SQLite
strings do not normally contain NUL characters, the length(X) function will
usually return the total number of characters in the string X. For a blob
value X, length(X) returns the number of bytes in the blob. If X is NULL
then length(X) is NULL. If X is numeric then length(X) returns the length
of a string representation of X. 
"""

	Note the last sentence for numerics. 


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list