views:

123

answers:

2

Hi, I'm using SQLite 3 with PHP.

The SQLite table consists of 2 INTEGER and one FLOAT column. When saving data into this table using PHP floats are not saved correctly (default value is stored instead). The two integer columns are saved. Any ideas what could be wrong? Thank you.

Simplified code that actually works correctly:

$conn = new SQLite3('dbFileName');
$conn->query("CREATE TABLE data (
       id INTEGER NOT NULL DEFAULT 0 ,
       ts INTEGER NOT NULL DEFAULT 0 ,
       value FLOAT NOT NULL DEFAULT 0
    );"
);
$conn->query("REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12.1')");

-> 1|1234567890|0

+2  A: 

This is just a suggestion, seeing as I have never used SQLite, but are you sure the numbers should be quoted? That seems somewhat odd to me.

Try:

$conn->query("REPLACE INTO data(id,ts,value) VALUES (1, 1234567890, 12.1)");
Atli
A: 

Sqlite data types are typeless, so your queries have to work:

all the following queries works for me

REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12.1');
REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12,123')
REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','abc');

Check your PHP variable, you might have a bug and you pass a null variable.

Pentium10
Thank you for your answer, I made a print of the SQL statement before calling $conn->query($sql): REPLACE INTO data (id,ts,value) VALUES ('493','1264590900','232.8')the result in the database was 493|1264590900|nullthe above code works correctly, but in the 'real' code I have some data acquisition code in between the 'new SQLite3()' and the '->query'
flurin
run your query in the database console, it works fine (maybe you have wrong create table, or if you use triggers check them out too)
Pentium10
yes, in the database console it works fine...what are triggers?
flurin
if you haven't heard of triggers, you don't have any. Google it.
Pentium10