codedgr/database

JSON辅助库

1.0.2 2018-11-09 10:57 UTC

This package is auto-updated.

Last update: 2024-09-10 01:13:54 UTC


README

控制器

扩展PDO类。

函数q($query, $args, &$stmt)

一次性执行pdo->prepare()pdo->execute()pdo->fetch()

函数import($pathToSqlFile)

.sql文件导入到数据库中。

函数dumb()

需要一个常量DATABASE_DUMP_PATH,指定所有转储文件存储的路径。

维护

需要一个包含2列的settings表,例如

CREATE TABLE `settings` (
  `key` varchar(255) DEFAULT NULL,
  `value` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `settings` VALUES ('database_version', '1');

需要一个常量DATABASE_UPDATE_FOLDER,指定所有.sql.php文件存储和使用的路径,以保持数据库更新。

查询

扩展控制器类。

用法

插入记录。

$values = [
    'firstName' => 'John',
    'lastName' => 'Doe',
];

$id = $db->insert('clients', $values);

插入记录,如果键重复则更新。

$values = [
    'firstName' => 'John',
    'lastName' => 'Doe',
];

$id = $db->insert('clients', $values, true);

插入记录,如果键重复则更新某些值。

$values = [
    'firstName' => 'John',
    'lastName' => 'Doe',
];

$update = [
    'lastName' => 'Doe',
];

$id = $db->insert('clients', $values, $update);

更新记录。

$values = [
    'firstName' => 'John',
    'lastName' => 'Doe',
];
$where = [
    'id' => 2
];

$rowsAffected = $db->update('clients', $values, $where);

选择记录。

$array = $db->select('clients', ['firstName' => 'John' ]);
$array = $db->select('clients', 'firstName = "John"']);

使用id选择记录。

$array = $db->select('clients', 2);

使用likenot like选择记录。

$array = $db->select('clients', ['firstName' => ['like', 'Jo%']]);

使用>=<=或其他条件选择记录。

$array = $db->select('clients', ['id' => ['>=', 2]]);

使用innot in选择记录。

$array = $db->select('clients', ['id' => ['in', [1,2,3]]]);

定义排序。

$array = $db->select('clients', ['order' => 'name desc']]);
$array = $db->select('clients', ['order' => 'name desc, id asc']]);

设置限制。

$array = $db->select('clients', ['limit' => 1]]);
$array = $db->select('clients', ['limit' => '1, 5']]);

删除记录。

$int = $db->delete('clients', $where]);

计数表中记录的数量。

$int = $db->count('clients', $where]);

获取某列的总和。

$int = $db->sum('age', 'clients', $where]);

获取某列的平均值。

$int = $db->avg('age', clients', $where]);

获取某列的最小值。

$int = $db->min('age', clients', $where]);

获取某列的最大值。

$int = $db->max('age', 'clients', $where]);