lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Feb 10, 2016 at 9:05 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> This little detail becomes "great" fun when you use numbers as integers in SQL statement:
>
> local id = tonumber(query.id)  -- Get the id from a string in a webform
> local res = db:execParams('select * from foo where id = $1::integer', id)
>
> In PostgreSQL this results in an error like "Invalid input syntax for type integer: 41.0".

What is funny is that 'tonumber' follows the syntax given to it. If it
is returning a float, it is because 'query.id' has a float format (either
a point or an exponent). Anyway, the correct approach is to follow
Daniel's suggestion.

-- Roberto
 
Would string.format("%u", a) be preferred over using type coercion with something like a|0