0%

数据库大作业笔记(二)

含有事务应用的删除操作

path:E:\Xampp\htdocs\yii2\sport_equipment\common\models\Equipment.php

public function delete()

​ {

​ if (!$this->isTransactional(self::OP_DELETE)) {

​ return $this->deleteInternal();

​ }

$db = Yii::$app->db;

$transaction = $db->beginTransaction();

try {

​ $db->createCommand(‘UPDATE post SET status=1 WHERE id=1’)->execute();

​ $result = $this->deleteInternal();

​ if ($result === false) {

​ $transaction->rollBack();

​ } else {

​ $transaction->commit();

​ }

​ } catch(\Exception $e) {

​ $transaction->rollBack();

​ throw $e;

​ } catch(\Throwable $e) {

​ $transaction->rollBack();

​ throw $e;

​ }

​ }

mySql获取当前时间
1
2
3
4
5
6
7
SELECT current_date(), curdate(), curdate() + 0;      # '2018-03-09' '2018-03-09'  20180309
SELECT current_time(), curtime(), curtime() + 0; # 16:49:41 16:49:41 164941
SELECT current_time(2), curtime(2), curtime(2) + 0; # 16:50:50 16:50:50 165050.01

SELECT now(), now(3), now(6);

2018-03-09 16:53:51 2018-03-09 16:53:51.163 2018-03-09 16:53:51.163791
数据库插入时报错

The SQL being executed was: INSERT INTO equipment (eqpid, catid) VALUES (‘c2002’, ‘c2’)

1
2
3
4
5
6
Error Info: Array
(
[0] => 21S01
[1] => 1136
[2] => Column count doesn't match value count at row 1
)

Caused by: PDOException

SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn’t match value count at row 1