lua-users home
lua-l archive

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


On Aug 5, 2010, at 2:25 PM, Florian Weimer wrote:

> * Doug Currie:
> 
>>> 	* dbvm_bind_index() should bind a double only if necessary:
>> 
>> Hmm, I'm not convinced. What is your rationale? Since Lua's number
>> type is (normally) double the extra test seems excessive.
> 
> SQLite has distinct types for integers and doubles and keeps them in
> the database.  This might become visible when you access the database
> using a different binding or from a different programming language.

Yes, but Lua has no distinct type for integers. 

Under what circumstances is it appropriate to "guess" that a LUA_TNUMBER is an integer? To me, just because a Lua number happens to have no fractional part doesn't make it an integer. For example, should a piece of Lua code that binds an SQLite3 parameter to an expression such as (x / y)  sometimes use sqlite3_bind_int and sometimes use sqlite3_bind_double depending on the result of the calculation?

You can control this conversion from the SQL side by including the conversion in the SQL statement:

~ e$ sqlite3
SQLite version 3.7.0
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table tbl (a integer, b real, c text);
sqlite> insert into tbl values (2.5, 2.5, 2.5);
sqlite> insert into tbl values (cast(2.5 as integer), cast(2.5 as real), cast(2.5 as string));
sqlite> select * from tbl;
2.5|2.5|2.5
2|2.5|2.5
sqlite> select typeof(a), typeof(b), typeof(c) from tbl;
real|real|text
integer|real|text

e