选择语言 :

 Driver_Database_Driver_SQLite::query

查询

$use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接

Database_Driver_SQLite_Result Driver_Database_Driver_SQLite::query( string $sql [, string $as_object = null , $use_master = null ] )

参数列表

参数 类型 描述 默认值
$sql string 查询语句
$as_object string 是否返回对象 null
$use_master unknown null
返回值
  • Database_Driver_SQLite_Result
File: ./drivers/database/sqlite/sqlite.class.php
public function query($sql, $as_object=null, $use_master = null)
{
    $sql = trim($sql);

    if (preg_match('#^([a-z]+)(:? |\n|\r)#i',$sql,$m))
    {
        $type = strtoupper($m[1]);
    }

    # 连接数据库
    $connection = $this->connection();

    # 记录调试
    if(IS_DEBUG)
    {
        Core::debug()->info($sql,'SQLite');

        static $is_sql_debug = null;

        if (null === $is_sql_debug) $is_sql_debug = (bool)Core::debug()->profiler('sql')->is_open();

        if ($is_sql_debug)
        {
            $db = $this->_get_hostname_by_connection_hash($this->connection_id());
            $benchmark = Core::debug()->profiler('sql')->start('Database', 'sqlite://'.$db);
        }
    }

    static $is_no_cache = null;
    if (null === $is_no_cache) $is_no_cache = (bool)Core::debug()->profiler('nocached')->is_open();
    //显示无缓存数据
    if ($is_no_cache && strtoupper(substr($sql, 0, 6)) == 'SELECT')
    {
        $sql = 'SELECT SQL_NO_CACHE' . substr($sql, 6);
    }

    // Execute the query
    if (($result = sqlite_query($sql, $connection)) === false)
    {
        if (isset($benchmark))
        {
            // This benchmark is worthless
            $benchmark->delete();
        }

        if (IS_DEBUG)
        {
            $err = 'Error:' . sqlite_error_string($connection) . '. SQL:' . $sql;
        }
        else
        {
            $err = sqlite_error_string($connection);
        }
        throw new Exception($err, sqlite_last_error($connection));
    }

    if (isset($benchmark))
    {
        # 在线查看SQL情况
        if ($is_sql_debug)
        {
            $data = array();
            $data[0]['db']            = $db;
            $data[0]['select_type']   = '';
            $data[0]['table']         = '';
            $data[0]['key']           = '';
            $data[0]['key_len']       = '';
            $data[0]['Extra']         = '';
            $data[0]['query']         = '';
            $data[0]['type']          = '';
            $data[0]['id']            = '';
            $data[0]['row']           = count($result);
            $data[0]['ref']           = '';
            $data[0]['all rows']      = '';
            $data[0]['possible_keys'] = '';

            if (strtoupper(substr($sql,0,6))=='SELECT')
            {
                $re = sqlite_query('EXPLAIN ' . $sql, $connection);
                $i = 0;
                while (true == ($row = sqlite_fetch_array($re , SQLITE_NUM)))
                {
                    $data[$i]['select_type']      = (string)$row[1];
                    $data[$i]['table']            = (string)$row[2];
                    $data[$i]['key']              = (string)$row[5];
                    $data[$i]['key_len']          = (string)$row[6];
                    $data[$i]['Extra']            = (string)$row[9];
                    if ($i==0) $data[$i]['query'] = '';
                    $data[$i]['type']             = (string)$row[3];
                    $data[$i]['id']               = (string)$row[0];
                    $data[$i]['ref']              = (string)$row[7];
                    $data[$i]['all rows']         = (string)$row[8];
                    $data[$i]['possible_keys']    = (string)$row[4];
                    $i ++;
                }
            }

            $data[0]['query'] = $sql;
        }
        else
        {
            $data = null;
        }

        Core::debug()->profiler('sql')->stop($data);
    }

    // Set the last query
    $this->last_query = $sql;

    if ($type === 'INSERT' || $type === 'REPLACE')
    {
        // Return a list of insert id and rows created
        return array
        (
            sqlite_last_insert_rowid($connection),
            sqlite_changes($connection)
        );
    }
    elseif ($type === 'UPDATE' || $type === 'DELETE')
    {
        // Return the number of rows affected
        return sqlite_changes($connection);
    }
    else
    {
        // Return an iterator of results
        return new Database_Driver_SQLite_Result($result, $sql, $as_object ,$this->config);
    }
}