du512926 2012-01-14 17:23
浏览 122

为什么所有数据类型都返回为带有PHP的sqlite3 fetchAll(PDO:FETCH_ASSOC)的字符串?

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"

A row from he results of the fetchAll is

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. 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.

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();
  • 写回答

1条回答 默认 最新

  • dongqian9013 2012-01-14 17:54
    关注

    This has nothing to do with 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.

    If you want the values to be of your desired types, you have to cast manually.

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?