SQLite Forum

.mode box / table improvement suggestion
Login

.mode box / table improvement suggestion

(1) By anonymous on 2020-08-18 13:18:08 [source]

When numbers (integers or floats) are involved we see this:

create table t(v);
insert into t values(111.11),(2.22),(3.32);
select * from t;

+--------+
|   v    |
+--------+
| 111.11 |
| 2.22   |
| 3.33   |
+--------+

SQLite3 already knows when a column has numeric affinity, be it integer or float. So, it could automatically use this to right align the column as if a .w -1 was used. We do not really need to know the actual length as the table/box mode will take care of that:

.w -1
select * from t;

gives a much nicer:

+--------+
|   v    |
+--------+
| 111.11 |
|   2.22 |
|   3.33 |
+--------+

If the numeric columns follow many other columns, one would have to use something like:

.w 0 0 0 0 ... 0 0 0 -1

to get the desired output, which is a bit awkward.

So, why not make it fully automatic while respecting a .w override, if used?

This would work great for integers but still not 100% for floats of varying length fractional part. Still better than being left aligned, though.

This last issue could be solved perhaps with another dot command, such as .float 2 to tell the system to default to 2 fractional digits. Or, even as a parameter to the table and box mode commands: .mode table 2