Constants

PARAM_FLOAT

PARAM_FLOAT = 21

Properties

$instance

$instance : 

Type

$PDOStatement

$PDOStatement : \PDOStatement

Type

\PDOStatement — PDO操作实例

$queryStr

$queryStr : string

Type

string — 当前SQL指令

$numRows

$numRows : 

Type

$transTimes

$transTimes : 

Type

$error

$error : 

Type

$links

$links : array<mixed,\PDO>

Type

array<mixed,\PDO> — 数据库连接ID 支持多个连接

$linkID

$linkID : \PDO

Type

\PDO — 当前连接ID

$linkRead

$linkRead : 

Type

$linkWrite

$linkWrite : 

Type

$fetchType

$fetchType : 

Type

$attrCase

$attrCase : 

Type

$event

$event : 

Type

$info

$info : 

Type

$builderClassName

$builderClassName : 

Type

$builder

$builder : 

Type

$config

$config : 

Type

$params

$params : 

Type

$breakMatchStr

$breakMatchStr : 

Type

$bind

$bind : 

Type

Methods

__construct()

__construct(array  $config = array()) 

架构函数 读取数据库配置信息

Parameters

array $config

数据库配置数组

instance()

instance(mixed  $config = array(), boolean|string  $name = false) : \think\db\Connection

取得数据库连接类实例

Parameters

mixed $config

连接配置

boolean|string $name

连接标识 true 强制重新连接

Throws

\think\Exception

Returns

\think\db\Connection

getBuilderClass()

getBuilderClass() : string

获取当前连接器类对应的Builder类

Returns

string

getBuilder()

getBuilder() : \think\db\Builder

获取当前的builder实例对象

Returns

\think\db\Builder

getFields()

getFields(string  $tableName) : array

取得数据表的字段信息

Parameters

string $tableName

Returns

array

getTables()

getTables(string  $dbName) : array

取得数据库的表信息

Parameters

string $dbName

Returns

array

fieldCase()

fieldCase(array  $info) : array

对返数据表字段信息进行大小写转换出来

Parameters

array $info

字段信息

Returns

array

getFieldBindType()

getFieldBindType(string  $type) : integer

获取字段绑定类型

Parameters

string $type

字段类型

Returns

integer

parseSqlTable()

parseSqlTable(string  $sql) : string

将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)

Parameters

string $sql

sql语句

Returns

string

getTableInfo()

getTableInfo(mixed  $tableName, string  $fetch = '') : mixed

获取数据表信息

Parameters

mixed $tableName

数据表名 留空自动获取

string $fetch

获取信息类型 包括 fields type bind pk

Returns

mixed

getPk()

getPk(string  $tableName) : string|array

获取数据表的主键

Parameters

string $tableName

数据表名

Returns

string|array

getTableFields()

getTableFields(string  $tableName) : array

获取数据表字段信息

Parameters

string $tableName

数据表名

Returns

array

getFieldsType()

getFieldsType(string  $tableName, string  $field = null) : array|string

获取数据表字段类型

Parameters

string $tableName

数据表名

string $field

字段名

Returns

array|string

getFieldsBind()

getFieldsBind(string  $tableName) : array

获取数据表绑定信息

Parameters

string $tableName

数据表名

Returns

array

getConfig()

getConfig(string  $config = '') : mixed

获取数据库的配置参数

Parameters

string $config

配置名称

Returns

mixed

setConfig()

setConfig(string|array  $config, mixed  $value = '') : void

设置数据库的配置参数

Parameters

string|array $config

配置名称

mixed $value

配置值

connect()

connect(array  $config = array(), integer  $linkNum, array|boolean  $autoConnection = false) : \PDO

连接数据库方法

Parameters

array $config

连接参数

integer $linkNum

连接序号

array|boolean $autoConnection

是否自动连接主数据库(用于分布式)

Throws

\think\Exception

Returns

\PDO

free()

free() 

释放查询结果

getPdo()

getPdo() : \PDO|false

获取PDO对象

Returns

\PDO|false

getCursor()

getCursor(string  $sql, array  $bind = array(), boolean  $master = false, \think\db\Model  $model = null, array  $condition = null, mixed  $relation = null) : \Generator

执行查询 使用生成器返回数据

Parameters

string $sql

sql指令

array $bind

参数绑定

boolean $master

是否在主服务器读操作

\think\db\Model $model

模型对象实例

array $condition

查询条件

mixed $relation

关联查询

Returns

\Generator

query()

query(string  $sql, array  $bind = array(), boolean  $master = false, boolean  $pdo = false) : array

执行查询 返回数据集

Parameters

string $sql

sql指令

array $bind

参数绑定

boolean $master

是否在主服务器读操作

boolean $pdo

是否返回PDO对象

Throws

\think\db\exception\BindParamException
\PDOException
\Exception
\Throwable

Returns

array

execute()

execute(string  $sql, array  $bind = array(), \think\db\Query  $query = null) : integer

执行语句

Parameters

string $sql

sql指令

array $bind

参数绑定

\think\db\Query $query

查询对象

Throws

\think\db\exception\BindParamException
\PDOException
\Exception
\Throwable

Returns

integer

find()

find(\think\db\Query  $query) : array|null|\PDOStatement|string

查找单条记录

Parameters

\think\db\Query $query

查询对象

Throws

\think\db\DbException
\think\db\ModelNotFoundException
\think\db\DataNotFoundException

Returns

array|null|\PDOStatement|string

cursor()

cursor(\think\db\Query  $query) : \Generator

使用游标查询记录

Parameters

\think\db\Query $query

查询对象

Returns

\Generator

select()

select(\think\db\Query  $query) : array|\PDOStatement|string

查找记录

Parameters

\think\db\Query $query

查询对象

Throws

\think\db\DbException
\think\db\ModelNotFoundException
\think\db\DataNotFoundException

Returns

array|\PDOStatement|string

insert()

insert(\think\db\Query  $query, boolean  $replace = false, boolean  $getLastInsID = false, string  $sequence = null) : integer|string

插入记录

Parameters

\think\db\Query $query

查询对象

boolean $replace

是否replace

boolean $getLastInsID

返回自增主键

string $sequence

自增序列名

Returns

integer|string

insertAll()

insertAll(\think\db\Query  $query, mixed  $dataSet = array(), boolean  $replace = false, integer  $limit = null) : integer|string

批量插入记录

Parameters

\think\db\Query $query

查询对象

mixed $dataSet

数据集

boolean $replace

是否replace

integer $limit

每次写入数据限制

Throws

\Exception
\Throwable

Returns

integer|string

selectInsert()

selectInsert(\think\db\Query  $query, string  $fields, string  $table) : integer|string

通过Select方式插入记录

Parameters

\think\db\Query $query

查询对象

string $fields

要插入的数据表字段名

string $table

要插入的数据表名

Throws

\think\exception\PDOException

Returns

integer|string

update()

update(\think\db\Query  $query) : integer|string

更新记录

Parameters

\think\db\Query $query

查询对象

Throws

\think\Exception
\think\exception\PDOException

Returns

integer|string

delete()

delete(\think\db\Query  $query) : integer

删除记录

Parameters

\think\db\Query $query

查询对象

Throws

\think\Exception
\think\exception\PDOException

Returns

integer

value()

value(\think\db\Query  $query, string  $field, boolean  $default = null) : mixed

得到某个字段的值

Parameters

\think\db\Query $query

查询对象

string $field

字段名

boolean $default

默认值

Returns

mixed

aggregate()

aggregate(\think\db\Query  $query, string  $aggregate, mixed  $field) : mixed

得到某个字段的值

Parameters

\think\db\Query $query

查询对象

string $aggregate

聚合方法

mixed $field

字段名

Returns

mixed

column()

column(\think\db\Query  $query, string  $field, string  $key = '') : array

得到某个列的数组

Parameters

\think\db\Query $query

查询对象

string $field

字段名 多个字段用逗号分隔

string $key

索引

Returns

array

pdo()

pdo(\think\db\Query  $query) : \PDOStatement|string

执行查询但只返回PDOStatement对象

Parameters

\think\db\Query $query

Returns

\PDOStatement|string

getRealSql()

getRealSql(string  $sql, array  $bind = array()) : string

根据参数绑定组装最终的SQL语句 便于调试

Parameters

string $sql

带参数绑定的sql语句

array $bind

参数绑定列表

Returns

string

transaction()

transaction(callable  $callback) : mixed

执行数据库事务

Parameters

callable $callback

数据操作方法回调

Throws

\think\exception\PDOException
\Exception
\Throwable

Returns

mixed

startTransXa()

startTransXa(string  $xid) : void

启动XA事务

Parameters

string $xid

XA事务id

prepareXa()

prepareXa(string  $xid) : void

预编译XA事务

Parameters

string $xid

XA事务id

commitXa()

commitXa(string  $xid) : void

提交XA事务

Parameters

string $xid

XA事务id

rollbackXa()

rollbackXa(string  $xid) : void

回滚XA事务

Parameters

string $xid

XA事务id

startTrans()

startTrans() : void

启动事务

Throws

\PDOException
\Exception

commit()

commit() : void

用于非自动提交状态下面的查询提交

Throws

\think\exception\PDOException

rollback()

rollback() : void

事务回滚

Throws

\think\exception\PDOException

batchQuery()

batchQuery(array  $sqlArray = array(), array  $bind = array()) : boolean

批处理执行SQL语句 批处理的指令都认为是execute操作

Parameters

array $sqlArray

SQL批处理指令

array $bind

参数绑定

Returns

boolean

getQueryTimes()

getQueryTimes(boolean  $execute = false) : integer

获得查询次数

Parameters

boolean $execute

是否包含所有查询

Returns

integer

getExecuteTimes()

getExecuteTimes() : integer

获得执行次数

Returns

integer

close()

close() : $this

关闭数据库(或者重新连接)

Returns

$this

getLastSql()

getLastSql() : string

获取最近一次查询的sql语句

Returns

string

getLastInsID()

getLastInsID(string  $sequence = null) : string

获取最近插入的ID

Parameters

string $sequence

自增序列名

Returns

string

getNumRows()

getNumRows() : integer

获取返回或者影响的记录数

Returns

integer

getError()

getError() : string

获取最近的错误信息

Returns

string

listen()

listen(callable  $callback) : void

监听SQL执行

Parameters

callable $callback

回调方法

log()

log(  $log,   $type = 'sql') 

Parameters

$log
$type

__destruct()

__destruct() 

析构方法

initialize()

initialize() : void

初始化

setBuilder()

setBuilder(\think\db\Builder  $builder) : void

设置当前的数据库Builder对象

Parameters

\think\db\Builder $builder

parseDsn()

parseDsn(array  $config) : string

解析pdo连接的dsn信息

Parameters

array $config

连接信息

Returns

string

getExplain()

getExplain(string  $sql) : array

SQL性能分析

Parameters

string $sql

Returns

array

bindValue()

bindValue(array  $bind = array()) : void

参数绑定 支持 ['name'=>'value','id'=>123] 对应命名占位符 或者 ['value',123] 对应问号占位符

Parameters

array $bind

要绑定的参数列表

Throws

\think\db\exception\BindParamException

bindParam()

bindParam(array  $bind) : void

存储过程的输入输出参数绑定

Parameters

array $bind

要绑定的参数列表

Throws

\think\db\exception\BindParamException

getResult()

getResult(boolean  $pdo = false, boolean  $procedure = false) : array

获得数据集数组

Parameters

boolean $pdo

是否返回PDOStatement

boolean $procedure

是否存储过程

Returns

array

procedure()

procedure() : array

获得存储过程数据集

Returns

array

supportSavepoint()

supportSavepoint() : boolean

是否支持事务嵌套

Returns

boolean

parseSavepoint()

parseSavepoint(  $name) : string

生成定义保存点的SQL

Parameters

$name

Returns

string

parseSavepointRollBack()

parseSavepointRollBack(  $name) : string

生成回滚到保存点的SQL

Parameters

$name

Returns

string

isBreak()

isBreak(\PDOException|\Exception  $e) : boolean

是否断线

Parameters

\PDOException|\Exception $e

异常对象

Returns

boolean

debug()

debug(boolean  $start, string  $sql = '', boolean  $master = false) : void

数据库调试 记录当前SQL及分析性能

Parameters

boolean $start

调试开始标记 true 开始 false 结束

string $sql

执行的SQL语句 留空自动获取

boolean $master

主从标记

triggerSql()

triggerSql(string  $sql, float  $runtime, mixed  $explain = array(), boolean  $master = false) : void

触发SQL事件

Parameters

string $sql

SQL语句

float $runtime

SQL运行时间

mixed $explain

SQL分析

boolean $master

主从标记

initConnect()

initConnect(boolean  $master = true) : void

初始化数据库连接

Parameters

boolean $master

是否主服务器

multiConnect()

multiConnect(boolean  $master = false) : \PDO

连接分布式服务器

Parameters

boolean $master

主服务器

Returns

\PDO

cacheData()

cacheData(string  $key, mixed  $data, array  $config = array()) 

缓存数据

Parameters

string $key

缓存标识

mixed $data

缓存数据

array $config

缓存参数

getCacheData()

getCacheData(\think\db\Query  $query, mixed  $cache,   $data,   $key = null) : mixed

获取缓存数据

Parameters

\think\db\Query $query

查询对象

mixed $cache

缓存设置

$data
$key

Returns

mixed

getCacheKey()

getCacheKey(\think\db\Query  $query, mixed  $value) : string

生成缓存标识

Parameters

\think\db\Query $query

查询对象

mixed $value

缓存数据

Returns

string