Constants

PARAM_FLOAT

PARAM_FLOAT = 21

Properties

$config

$config : array

数据库连接参数配置

Type

array

$PDOStatement

$PDOStatement : \PDOStatement

PDO操作实例

Type

\PDOStatement —

$queryStr

$queryStr : string

当前SQL指令

Type

string

$transTimes

$transTimes : int

事务指令数

Type

int

$reConnectTimes

$reConnectTimes : int

重连次数

Type

int

$fetchType

$fetchType : int

查询结果类型

Type

int

$attrCase

$attrCase : int

字段属性大小写

Type

int

$info

$info : array

数据表信息

Type

array

$queryStartTime

$queryStartTime : float

查询开始时间

Type

float

$params

$params : array

PDO连接参数

Type

array

$bindType

$bindType : array

参数绑定类型映射

Type

array

$breakMatchStr

$breakMatchStr : array

服务器断线标识字符

Type

array

$bind

$bind : array

绑定参数

Type

array

$numRows

$numRows : int

返回或者影响记录数

Type

int

$error

$error : string

错误信息

Type

string

$links

$links : array

数据库连接ID 支持多个连接

Type

array

$linkID

$linkID : object

当前连接ID

Type

object

$linkRead

$linkRead : object

当前读连接ID

Type

object

$linkWrite

$linkWrite : object

当前写连接ID

Type

object

$builder

$builder : \think\db\Builder

Builder对象

Type

Builder

$db

$db : \think\DbManager

Db对象

Type

DbManager

$readMaster

$readMaster : bool

是否读取主库

Type

bool

$cache

$cache : \Psr\SimpleCache\CacheInterface

缓存对象

Type

CacheInterface

Methods

getQueryClass()

getQueryClass() : string

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

Returns

string —

getBuilderClass()

getBuilderClass() : string

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

Returns

string —

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) : int

获取字段绑定类型

Parameters

string $type

字段类型

Returns

int —

getSchemaInfo()

getSchemaInfo(string  $tableName, bool  $force = false) : array

Parameters

string $tableName

数据表名称

bool $force

强制从数据库获取

Returns

array —

getTableInfo()

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

获取数据表信息

Parameters

mixed $tableName

数据表名 留空自动获取

string $fetch

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

Returns

mixed —

getTableFieldsInfo()

getTableFieldsInfo(string  $tableName) : array

获取数据表的字段信息

Parameters

string $tableName

数据表名

Returns

array —

getPk()

getPk(mixed  $tableName) : string|array

获取数据表的主键

Parameters

mixed $tableName

数据表名

Returns

string|array —

getAutoInc()

getAutoInc(mixed  $tableName) : string

获取数据表的自增主键

Parameters

mixed $tableName

数据表名

Returns

string —

getTableFields()

getTableFields(mixed  $tableName) : array

获取数据表字段信息

Parameters

mixed $tableName

数据表名

Returns

array —

getFieldsType()

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

获取数据表字段类型

Parameters

mixed $tableName

数据表名

string $field

字段名

Returns

array|string —

getFieldsBind()

getFieldsBind(mixed  $tableName) : array

获取数据表绑定信息

Parameters

mixed $tableName

数据表名

Returns

array —

connect()

connect(array  $config = [], int  $linkNum, array|bool  $autoConnection = false) : \PDO

连接数据库方法

Parameters

array $config

连接参数

int $linkNum

连接序号

array|bool $autoConnection

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

Throws

\think\db\exception\PDOException

Returns

\PDO —

view()

view(array  ...$args) : \think\db\BaseQuery

视图查询

Parameters

array $args variadic

Returns

\think\db\BaseQuery —

free()

free() : void

释放查询结果

getPdo()

getPdo() : \PDO|false

获取PDO对象

Returns

\PDO|false —

getCursor()

getCursor(\think\db\BaseQuery  $query, string  $sql, array  $bind = [], \think\Model  $model = null, array  $condition = null) : \Generator

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

Parameters

\think\db\BaseQuery $query

查询对象

string $sql

sql指令

array $bind

参数绑定

\think\Model $model

模型对象实例

array $condition

查询条件

Returns

\Generator —

query()

query(string  $sql, array  $bind = [], bool  $master = false) : array

执行查询 返回数据集

Parameters

string $sql

sql指令

array $bind

参数绑定

bool $master

主库读取

Throws

\think\db\exception\BindParamException
\PDOException

Returns

array —

execute()

execute(string  $sql, array  $bind = []) : int

执行语句

Parameters

string $sql

sql指令

array $bind

参数绑定

Throws

\think\db\exception\BindParamException
\PDOException

Returns

int —

pdo()

pdo(\think\db\BaseQuery  $query) : \PDOStatement

执行查询但只返回PDOStatement对象

Parameters

\think\db\BaseQuery $query

查询对象

Returns

\PDOStatement —

getPDOStatement()

getPDOStatement(string  $sql, array  $bind = [], bool  $master = false, bool  $procedure = false) : \PDOStatement

执行查询但只返回PDOStatement对象

Parameters

string $sql

sql指令

array $bind

参数绑定

bool $master

是否在主服务器读操作

bool $procedure

是否为存储过程调用

Throws

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

Returns

\PDOStatement —

find()

find(\think\db\BaseQuery  $query) : array

查找单条记录

Parameters

\think\db\BaseQuery $query

查询对象

Throws

\think\db\exception\DbException

Returns

array —

cursor()

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

使用游标查询记录

Parameters

\think\db\BaseQuery $query

查询对象

Returns

\Generator —

select()

select(\think\db\BaseQuery  $query) : array

查找记录

Parameters

\think\db\BaseQuery $query

查询对象

Throws

\think\db\exception\DbException

Returns

array —

insert()

insert(\think\db\BaseQuery  $query, bool  $getLastInsID = false) : mixed

插入记录

Parameters

\think\db\BaseQuery $query

查询对象

bool $getLastInsID

返回自增主键

Returns

mixed —

insertAll()

insertAll(\think\db\BaseQuery  $query, mixed  $dataSet = [], int  $limit) : int

批量插入记录

Parameters

\think\db\BaseQuery $query

查询对象

mixed $dataSet

数据集

int $limit

每次写入数据限制

Throws

\Exception
\Throwable

Returns

int —

selectInsert()

selectInsert(\think\db\BaseQuery  $query, array  $fields, string  $table) : int

通过Select方式插入记录

Parameters

\think\db\BaseQuery $query

查询对象

array $fields

要插入的数据表字段名

string $table

要插入的数据表名

Throws

\think\db\exception\PDOException

Returns

int —

update()

update(\think\db\BaseQuery  $query) : int

更新记录

Parameters

\think\db\BaseQuery $query

查询对象

Throws

\think\db\exception\PDOException

Returns

int —

delete()

delete(\think\db\BaseQuery  $query) : int

删除记录

Parameters

\think\db\BaseQuery $query

查询对象

Throws

\think\db\exception\PDOException

Returns

int —

value()

value(\think\db\BaseQuery  $query, string  $field, mixed  $default = null, bool  $one = true) : mixed

得到某个字段的值

Parameters

\think\db\BaseQuery $query

查询对象

string $field

字段名

mixed $default

默认值

bool $one

返回一个值

Returns

mixed —

aggregate()

aggregate(\think\db\BaseQuery  $query, string  $aggregate, mixed  $field, bool  $force = false) : mixed

得到某个字段的值

Parameters

\think\db\BaseQuery $query

查询对象

string $aggregate

聚合方法

mixed $field

字段名

bool $force

强制转为数字类型

Returns

mixed —

column()

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

得到某个列的数组

Parameters

\think\db\BaseQuery $query

查询对象

string $column

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

string $key

索引

Returns

array —

getRealSql()

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

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

Parameters

string $sql

带参数绑定的sql语句

array $bind

参数绑定列表

Returns

string —

transaction()

transaction(callable  $callback) : mixed

执行数据库事务

Parameters

callable $callback

数据操作方法回调

Throws

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

Returns

mixed —

startTrans()

startTrans() : void

启动事务

Throws

\PDOException
\Exception

commit()

commit() : void

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

Throws

\think\db\exception\PDOException

rollback()

rollback() : void

事务回滚

Throws

\think\db\exception\PDOException

batchQuery()

batchQuery(\think\db\BaseQuery  $query, array  $sqlArray = [], array  $bind = []) : bool

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

Parameters

\think\db\BaseQuery $query

查询对象

array $sqlArray

SQL批处理指令

array $bind

参数绑定

Returns

bool —

close()

close() : $this

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

Returns

$this —

getLastSql()

getLastSql() : string

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

Returns

string —

getLastInsID()

getLastInsID(\think\db\BaseQuery  $query, string  $sequence = null) : mixed

获取最近插入的ID

Parameters

\think\db\BaseQuery $query

查询对象

string $sequence

自增序列名

Returns

mixed —

getError()

getError() : string

获取最近的错误信息

Returns

string —

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

__construct()

__construct(array  $config = []) : mixed

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

Parameters

array $config

数据库配置数组

Returns

mixed —

getBuilder()

getBuilder() : \think\db\Builder

获取当前的builder实例对象

Returns

\think\db\Builder —

newQuery()

newQuery() : mixed

创建查询对象

Returns

mixed —

table()

table( $table) : \think\db\BaseQuery

指定表名开始查询

Parameters

$table

Returns

\think\db\BaseQuery —

name()

name( $name) : \think\db\BaseQuery

指定表名开始查询(不带前缀)

Parameters

$name

Returns

\think\db\BaseQuery —

setDb()

setDb(\think\DbManager  $db) : void

设置当前的数据库Db对象

Parameters

\think\DbManager $db

setCache()

setCache(\Psr\SimpleCache\CacheInterface  $cache) : void

设置当前的缓存对象

Parameters

\Psr\SimpleCache\CacheInterface $cache

getCache()

getCache() : \Psr\SimpleCache\CacheInterface|null

获取当前的缓存对象

Returns

\Psr\SimpleCache\CacheInterface|null —

getConfig()

getConfig(string  $config = '') : mixed

获取数据库的配置参数

Parameters

string $config

配置名称

Returns

mixed —

getNumRows()

getNumRows() : int

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

Returns

int —

__destruct()

__destruct() : mixed

析构方法

Returns

mixed —

parseDsn()

parseDsn(array  $config) : string

解析pdo连接的dsn信息

Parameters

array $config

连接信息

Returns

string —

getFieldType()

getFieldType(string  $type) : string

获取字段类型

Parameters

string $type

字段类型

Returns

string —

getSchemaCacheKey()

getSchemaCacheKey(string  $schema) : string

获取数据表信息缓存key

Parameters

string $schema

数据表名称

Returns

string —

createPdo()

createPdo( $dsn,  $username,  $password,  $params) : \PDO

创建PDO实例

Parameters

$dsn
$username
$password
$params

Returns

\PDO —

pdoQuery()

pdoQuery(\think\db\BaseQuery  $query, mixed  $sql, array  $bind = [], bool  $master = null) : array

执行查询 返回数据集

Parameters

\think\db\BaseQuery $query

查询对象

mixed $sql

sql指令

array $bind

参数绑定

bool $master

主库读取

Throws

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

Returns

array —

pdoExecute()

pdoExecute(\think\db\BaseQuery  $query, string  $sql, array  $bind = [], bool  $origin = false) : int

执行语句

Parameters

\think\db\BaseQuery $query

查询对象

string $sql

sql指令

array $bind

参数绑定

bool $origin

是否原生查询

Throws

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

Returns

int —

queryPDOStatement()

queryPDOStatement(\think\db\BaseQuery  $query, string  $sql, array  $bind = []) : \PDOStatement

Parameters

\think\db\BaseQuery $query
string $sql
array $bind

Returns

\PDOStatement —

bindValue()

bindValue(array  $bind = []) : 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(bool  $procedure = false) : array

获得数据集数组

Parameters

bool $procedure

是否存储过程

Returns

array —

procedure()

procedure() : array

获得存储过程数据集

Returns

array —

supportSavepoint()

supportSavepoint() : bool

是否支持事务嵌套

Returns

bool —

parseSavepoint()

parseSavepoint(string  $name) : string

生成定义保存点的SQL

Parameters

string $name

标识

Returns

string —

parseSavepointRollBack()

parseSavepointRollBack(string  $name) : string

生成回滚到保存点的SQL

Parameters

string $name

标识

Returns

string —

isBreak()

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

是否断线

Parameters

\PDOException|\Exception $e

异常对象

Returns

bool —

autoInsIDType()

autoInsIDType(\think\db\BaseQuery  $query, string  $insertId) : mixed

获取最近插入的ID

Parameters

\think\db\BaseQuery $query

查询对象

string $insertId

自增ID

Returns

mixed —

initConnect()

initConnect(bool  $master = true) : void

初始化数据库连接

Parameters

bool $master

是否主服务器

multiConnect()

multiConnect(bool  $master = false) : \PDO

连接分布式服务器

Parameters

bool $master

主服务器

Returns

\PDO —

trigger()

trigger(string  $sql = '', bool  $master = false) : void

数据库SQL监控

Parameters

string $sql

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

bool $master

主从标记

cacheData()

cacheData(\think\db\CacheItem  $cacheItem) : mixed

缓存数据

Parameters

\think\db\CacheItem $cacheItem

缓存Item

Returns

mixed —

getCacheKey()

getCacheKey(\think\db\BaseQuery  $query, string  $method = '') : string

分析缓存Key

Parameters

\think\db\BaseQuery $query

查询对象

string $method

查询方法

Returns

string —

parseCache()

parseCache(\think\db\BaseQuery  $query, array  $cache, string  $method = '') : \think\db\CacheItem

分析缓存

Parameters

\think\db\BaseQuery $query

查询对象

array $cache

缓存信息

string $method

查询方法

Returns

\think\db\CacheItem —