开发者

Floats are not saved correctly into SQLite table using PHP

开发者 https://www.devze.com 2022-12-18 09:52 出处:网络
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).

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 开发者_如何学PythonTABLE 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


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)");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消