[Qgis-developer] Re: QGIS API: change layer field length

a.furieri at lqt.it a.furieri at lqt.it
Fri Jul 15 07:38:06 EDT 2011


Hi Romain,
the only way I can imagine so to retrieve
field lengths and types is by "sniffing"
them on a first preliminary analysis pass.

You can easily do such a thing using few SQL
queries: and the basic tools to be used are
the ones I already reported on my previous post.

Pass I:
-------
SELECT DISTINCT typeof(col-1), ..., typeof(col-N) 
FROM tbl-name;
  
so you can get the data-type(s) actually stored
into the table/layer.
obviously when mixed-types are found for the same
column, some carefully planned format transposition
is required (i.e. promoting INTEGERs to FLOATs,
transforming numbers into TEXT ...)

Pass II:
--------
SELECT Max(length(col-1)), ..., Max(length(col-N)
FROM tbl-name
WHERE typeof(col-1) = 'text' AND ...
  typeof(col-N) = 'text';

so you can get max-length for any text-type column

----
both them are plain SQL queries, so I suppose
it would be absolutely easy execute them from 
your Python code.

bye Sandro



More information about the Qgis-developer mailing list