php - How to check if db execute sql fails? -
i use console command in yii2 framework , want import db schema file via execute() method:
public function actionreset() { $schemaspath = yii::getalias('@common') . directory_separator . 'data' . directory_separator; $schemafile = $schemaspath . 'schema.sql'; try { $command = yii::$app->db->createcommand(file_get_contents($schemafile)); $command->execute(); console::output('schema imported.'); return self::exit_code_normal; } catch (\exception $e) { console::error($e->getmessage()); } return self::exit_code_error; }
documentation says:
this method should used executing non-query sql statement, such insert, delete, update sqls. no result set returned.
run as:
$ ./yii db/reset
produce no error/exceptions.
run as:
$ mysql -u{user} -p -hlocalhost -d{base} < schema.sql
produce:
error 1064 (42000) @ line 241: have error in sql syntax; check manual corresponds mariadb server version right syntax use near 'json default null ) engine=innodb default charset=utf8 collate=utf8_unicode_ci' @ line 4
because mariadb 10.1 don't support json data type. so, how check if queries/commands $schemafile success? trying find yii::$app->db->getlasterror() - no result. hm, ideas?
i found solution here: https://stackoverflow.com/a/23258691/1829368, it's:
try { $command = yii::$app->db->createcommand(file_get_contents($schemafile)); $command->execute(); { } while ($command->pdostatement->nextrowset()); console::output('schema imported.'); return self::exit_code_normal; } catch (\exception $e) { console::error($e->getmessage()); }
a do-while loop nextrowset() trigger \pdoexception when first db query fails.
Comments
Post a Comment