Android Question sqlite question

Charles

Member
Licensed User
Can anyone tell me why this query
SELECT printf("$ %4.2f",total) FROM tickets WHERE total > 0;
where total is type real,
works fine ($1.00) when I run it in a sqlite db manager but when I build the exact same string in B4A using stringbuilder, it fails on the same db ?
 

DonManfred

Expert
Licensed User
Which version of sqlite you are using?

printf was added as a core function in version 3.8.3 of sqlite3. This information can be found in these release notes:


2014-02-03 (3.8.3)

Added support for common table expressions and the WITH clause.

Added the printf() SQL function.
 

Charles

Member
Licensed User
I know. I'm using a function to format the returned field before plugging it into webview.
I was just curious why it errors out while printf is a legal sqlite function.
 

Mahares

Well Known Member
Licensed User
I was just curious why it errors out while printf is a legal sqlite function.
As Manfred indicated, the printf function was introduced in version 3.8.3 of SQLite. If your version is lower, it will not work. It is not forward compatible. My device has a version of: 3.8.6. and the function works for me; see my below code:
B4X:
txt="SELECT printf('$ %4.2f',total) AS F_total FROM tickets WHERE total > 0"
Cursor1=SQL1.ExecQuery(txt)
        For i=0 To Cursor1.RowCount-1
        Cursor1.Position=i
            Log (Cursor1.GetString("F_total") )      'example: displays: $ 4.20 or $ 8234.00
        Next
 
Top