On 11/28/19 10:24 AM, Dominique Devienne wrote:
> Obviously it's ugly to use concatenation and char() to format a string
> literal with tabs and newlines.
> Is there a better way? Why doesn't printf() support newlines and tabs like
> it's C cousin? --DD
>
> PS: Built-in printf() also doesn't support positional params, to "emulate"
> newline with printf( '%1$s1: %2$s%1$s2: %3$s%1$s' , char(10), 'one',
> 'two'), but that's not too readable either, in any case
>
> sqlite> select printf('\n1: %s\n2: %s\n', 'one', 'two');
> \n1: one\n2: two\n
> sqlite> select char(10)||printf('1: %s', 'one')||char(10)||printf('2: %s',
> 'two')||char(10);
>
> 1: one
> 2: two
>
> sqlite> select printf('\t1: %s\t2: %s\t', 'one', 'two');
> \t1: one\t2: two\t
> sqlite> select char(9)||printf('1: %s', 'one')||char(9)||printf('2: %s',
> 'two')||char(9);
>         1: one  2: two
> sqlite>

A couple of things to note:

\n and \t are not 'printf' features, but C string features, that \ is an
escape introducer for compiling a string, and if followed by a letter
like n or t it builds a string with the special value represented by
that function. The \n does NOT make it into the string itself, only the
code for a newline. You are not typing your line into a C compiler, but
the sqlite shell program, so it build strings differently. 

ISO C also does not support positional arguments, that is an extension
that many compiler provide as I believe it is a common extension used in
linux.

You also don't NEED positional arguements, you could use

printf('%s1: %s%s2: %s', char(10), 'one', char(10), 'two')


There is a discusson of why SQLite uses its own printf here:
https://sqlite.org/printf.html

-- 
Richard Damon

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to