2011-09-09 107 views
0

列的當前值countProductSelect和countProductInput - INTEGER NOT NULL;無法從SQLite中新創建的列中獲取數據

需要使INTEGER DEFAULT NULL;

下面的PHP代碼。

的問題是,當試圖更新數據和countProductSelect countProductInput,從時間列和countProductSelectTmp countProductInputTmp,得到錯誤 - 「沒有這樣的列:countProductSelectTmp」。

如何處理這個錯誤?或者也許有更多受過教育的算法來解決原始問題?

/** 
* Updating the database 
* 
* @return bool 
*/ 
protected function _updateDB() 
{ 
    $version = '3.8.4.0'; 

    $this->_pdo->exec(' 
     ALTER TABLE data ADD optionalCountProduct INTEGER DEFAULT NULL; 
     ALTER TABLE data ADD countProductSelectTmp INTEGER DEFAULT NULL; 
     ALTER TABLE data ADD countProductInputTmp INTEGER DEFAULT NULL; 
     UPDATE data SET countProductSelectTmp = countProductSelect, countProductInputTmp = countProductInput; 
    '); 

    $this->_deleteSQLiteColumn(array('countProductSelect', 'countProductInput')); 

    // BUG 
    // countProductSelect and countProductInput can not get the value countProductSelectTmp and countProductInputTmp 
    $this->_pdo->exec(' 
     ALTER TABLE data ADD countProductSelect INTEGER DEFAULT NULL; 
     ALTER TABLE data ADD countProductInput INTEGER DEFAULT NULL; 
     UPDATE data SET countProductSelect = countProductSelectTmp, countProductInput = countProductInputTmp; 
     UPDATE version SET id = "' . $version . '"; 
    '); 

    $this->_deleteSQLiteColumn(array('countProductSelectTmp', 'countProductInputTmp')); 

    return true; 
} 


/** 
* Remove column from a SQLite Table 
* 
* @param array $column name of the column to remove 
* @return bool 
*/ 
protected function _deleteSQLiteColumn(array $column) 
{ 
    return (bool)$this->_pdo->exec(' 
     BEGIN TRANSACTION; 
     CREATE TEMPORARY TABLE backup(' . $this->_getFullColumnsString() . '); 
     INSERT INTO backup SELECT ' . $this->_getShortColumnsString() . ' FROM data; 
     DROP TABLE data; 
     CREATE TABLE data(' . $this->_getFullColumnsString($column) . '); 
     INSERT INTO data SELECT ' . $this->_getShortColumnsString($column) . ' FROM backup; 
     DROP TABLE backup; 
     COMMIT; 
    '); 
} 

回答

0

fine manual

PDO :: EXEC - 執行SQL語句並返回受影響的行

的數量,但你想在一次執行多個語句所以也許它只是執行最後的聲明。使用exec按預期嘗試和執行的語句一次一個:

$this->_pdo->exec('ALTER TABLE data ADD optionalCountProduct INTEGER DEFAULT NULL'); 
$this->_pdo->exec('ALTER TABLE data ADD countProductSelectTmp INTEGER DEFAULT NULL'); 
$this->_pdo->exec('ALTER TABLE data ADD countProductInputTmp INTEGER DEFAULT NULL'); 
$this->_pdo->exec('UPDATE data SET countProductSelectTmp = countProductSelect, countProductInputTmp = countProductInput'); 

然後再與你的下一個多語句exec和你_deleteSQLiteColumn

只要一個更好的方法,看起來你想要做的就是放棄NOT NULL約束,但SQLite's ALTER TABLE是有點限制,所以你的「添加列,複製列,通過表複製刪除列」方法是最好的你可以做;從SQLite FAQ

例如,假設你有一個名爲「T1」的列名「A」,「B」表,「C」,而你想從該表中刪除列「C」 。以下步驟說明如何做到這一點:

BEGIN TRANSACTION; 
CREATE TEMPORARY TABLE t1_backup(a,b); 
INSERT INTO t1_backup SELECT a,b FROM t1; 
DROP TABLE t1; 
CREATE TABLE t1(a,b); 
INSERT INTO t1 SELECT a,b FROM t1_backup; 
DROP TABLE t1_backup; 
COMMIT; 
+0

問題依然存在。仍然需要幫助。 – Gemorroj

+0

@Gemorroj:你的新版本是什麼樣的?當我早上回來時,我會看看。 –