2012-12-02 107 views

回答

1

在從MySQL異常的情況下運行查詢,

$insertCommand = $model->getCommandBuilder()->createSqlCommand($sql,$baseParams+$relParams); 
try { 
     $insertCommand->execute(); 
} catch(CDbException $e) { 
     // Here's a way to get to the error code for the statement in question. 
     // These codes are standardized ANSI SQL "SQLSTATE" error codes. 
     $sqlErrorCode = $insertCommand->pdoStatement->errorCode(); 
     // And to get to the class part of it, simple grab the two first characters. 
     // The class should be the same regardless of DB vendor, while the rest of the code can differ. 
     // For example one particular error was reported by PostgreSQL as 23505 but MySQL only said 23000. 
     $sqlErrorCodeClass = substr($sqlErrorCode, 0, 2); 
} 
+0

對於每一個查詢,我必須寫試試catch? –

相關問題