在简单示例中,Duplicate Key不会触发复合键的PDO异常抛出

时间:2015-04-17 01:36:43

标签: php pdo

这里是我的简单课程

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
class Test{
        private $dsn = 'mysql:host=localhost;dbname=scem;';
        private $db;
        function __construct()
        {
            $this->db = new PDO($this->dsn, "user", "pw");
            try {
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

            } catch (PDOException $e) {
                die("Unable to connect to DB with user / pass " . $e);
            }
        }

            public function doTest(){
                try {
                    $pdoSql = "INSERT INTO pscoperation_history VALUES (?,?,?,?,?,?,?,?,?,?,?)";// ON DUPLICATE KEY UPDATE pscclass = ?";
                    $stmt = $this->db->prepare($pdoSql);
                    $result = $stmt->execute(array("2015-04-17 03:01:09", 519, 0, 0, "9999", "15", null, null, null, null, null, "15"));
                } catch (PDOException $ex) {
                    ob_end_flush();
                    echo($ex->getMessage());
                    flush();
                    ob_start();
                    die("error trying to insert");
                }
        }
}

$test = new Test();
$test->doTest();

我桌子的结构

--
-- Table structure for table `pscoperation_history`
--

CREATE TABLE IF NOT EXISTS `pscoperation_history` (
  `version_stamp` datetime NOT NULL,
  `pscoperation_id` int(11) unsigned NOT NULL,
  `is_realversion` tinyint(4) NOT NULL DEFAULT '0',
  `is_deleted` tinyint(4) NOT NULL DEFAULT '0',
  `version_author` int(11) NOT NULL,
  `pscclass` int(11) unsigned DEFAULT NULL,
  `visibility` enum('+','#','-','~') DEFAULT NULL,
  `pscopname` varchar(50) DEFAULT NULL,
  `returndatatype` varchar(50) DEFAULT NULL,
  `pscopmodifier` int(11) unsigned DEFAULT NULL,
  `pscopnote` text,
  PRIMARY KEY (`version_stamp`,`pscoperation_id`),
  KEY `pscclass` (`pscclass`),
  KEY `pscoperation_id` (`pscoperation_id`),
  KEY `version_stamp` (`version_stamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 11264 kB; (`project`) REFER `scem/projects`(`pr';

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

问题:为什么这不会触发重复键错误?它执行没有任何问题。

0 个答案:

没有答案