简体   繁体   English

为什么所有数据类型都以字符串形式返回,并使用 PHP 使用 sqlite3 fetchAll(PDO:FETCH_ASSOC)?

[英]Why are all data types returned as strings with sqlite3 fetchAll(PDO:FETCH_ASSOC) with PHP?

I am using我在用

$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

to get all the rows of my table.获取我表的所有行。

The schema is defined as "id INTEGER PRIMARY KEY, title TEXT, year INTEGER, price REAL"模式定义为“id INTEGER PRIMARY KEY,title TEXT,year INTEGER,price REAL”

A row from he results of the fetchAll is fetchAll 结果中的一行是

array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(14) "The Dark Night"
    [2]=>
    string(4) "2008"
    [3]=>
    string(5) "19.95"
  }

Why are all the data types being returned as strings?为什么所有数据类型都以字符串形式返回? I want them to be returned as defined in the schema.我希望它们按照架构中的定义返回。 I understand the 'typeless' nature of SQLite, but they do define the limited data types as TEXT, INTEGER, and REAL.我理解 SQLite 的“无类型”性质,但他们确实将有限的数据类型定义为 TEXT、INTEGER 和 REAL。 How can I get the data to be returned with the specified data types?如何获取指定数据类型返回的数据? I don't want to iterate through each row, and convert it with PHP - that just seems to be too slow.我不想遍历每一行,并用 PHP 转换它——这似乎太慢了。

The Complete Test Code as Follows:完整的测试代码如下:

<?
class sqlite_test {
    function __construct()
    {
        $this->dbase_filename = "test.sqlite";
        $this->init_dbase();

    }

    function init_dbase()
    {
        $this->pdo = new PDO("sqlite:".$this->dbase_filename);
    }

    function open_table()
    {
        $table = "dvds";

        if ( ! ($test_to_see_if_table_exists = $this->pdo->query("SELECT 1 from $table")) ) {
            $schema = "id INTEGER PRIMARY KEY, title TEXT, year INTEGER, price REAL";
            $query = "CREATE TABLE $table ($schema)";

            $this->pdo->exec($query);
        }

        return $test_to_see_if_table_exists;
    }

    function add_test_records()
    {
        $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Dark Night", 2008, 19.95)';
        $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Wizard of Oz", 1939, 9.95)';
        $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "Jaws", 1977, 6.95)';

        $this->pdo->exec( join(';', $query) );
    }

    function dump_test_records()
    {
        $query = "SELECT * FROM dvds";

        if ($statement = $this->pdo->prepare($query)) {
            $statement->execute();

            $rows = $statement->fetchAll(PDO::FETCH_ASSOC);

            echo "<pre>";
            var_dump($rows);
            echo "</pre>";
        }
    }

    function main()
    {
        if ( ! $this->open_table() ) {
            $this->add_test_records();
        }

        $this->dump_test_records();
    }
}

$test = new sqlite_test();
$test->main();

This has nothing to do with SQLite; 这与SQLite无关; all records will be returned as collections of strings no matter what you use to query a database. 无论您使用什么来查询数据库,所有记录都将作为字符串集合返回。 It's just the nature of the database adapters/drivers used by PHP. 这只是PHP使用的数据库适配器/驱动程序的本质。

If you want the values to be of your desired types, you have to cast manually. 如果您希望值为所需类型,则必须手动进行投射。

PHP 8.1 开始为整数字段(sqlite pdo)返回数字而不是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM