如何处理SQLIte中的列默认值插入?

时间:2015-09-04 06:22:11

标签: php sqlite pdo

下面是表格结构。插入查询和错误消息。

表格结构

CREATE TABLE d0000ke_c_name (
    c_name INTEGER PRIMARY KEY NOT NULL,
    value VARCHAR(255) ,
    sc_version INTEGER ,
    sc_state INTEGER 
);

插入查询

$sql  = "INSERT INTO d0000kv_c_name SELECT 3 AS 'c_name','manash' AS 'value'   UNION SELECT 2,'ram' UNION SELECT 1,'rahim'";
$statement = $this->odb->prepare($sql);
if (!$statement) {
    echo "\nPDO::errorInfo():\n";
    print_r($this->odb->errorInfo());
}

$statement->execute();

错误消息

  

PDO :: errorInfo():数组([0] => HY000 [1] => 1 [2] =>表d0000kv_c_name有4列,但提供了2个值)

1 个答案:

答案 0 :(得分:2)

您的选择SELECT 3 AS 'c_name','manash' AS 'value' UNION SELECT 2,'ram' UNION SELECT 1,'rahim'返回2列,但您的表格d0000ke_c_name有4列。

您必须在INSERT INTO

中声明要填充的字段
$sql  = "INSERT INTO d0000kv_c_name (c_name, value) SELECT 3 AS 'c_name','manash' AS 'value' UNION SELECT 2,'ram' UNION SELECT 1,'rahim'";

(在这种情况下,字段sc_versionsc_state将获得默认值。)

或者您应该将缺少的字段(例如" 0,0和#34;字段sc_versionsc_state)添加到SELECT

$sql  = "INSERT INTO d0000kv_c_name SELECT 3 AS 'c_name','manash' AS 'value', 0, 0 UNION SELECT 2, 'ram', 0, 0 UNION SELECT 1,'rahim', 0, 0";