• 30th, Apr 2011

Zend Db exec() to import a .sql File

SQLSTATE[HY000]: General error: 1005 Can’t create table ‘database.table’ (errno: 150)

Case: When I try to import a batch file doing this…
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap(‘db’);
$dbAdapter = $bootstrap->getResource(‘db’);
$schemaSql = file_get_contents(dirname(__FILE__) . ‘/schema.mysql.sql’);
$dbAdapter->getConnection()->exec($schemSql);

Solution:
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap(‘db’);
$config = $bootstrap->getResource(‘db’)->getConfig();
$runCommand = “mysql -h “.$config["host"].” -P “.$config["port"].” -u’”.$config["username"].”‘ -p’”.$config["password"].”‘ “.$config["dbname"].” < “.dirname(__FILE__) . ‘/schema.mysql.sql’;
echo $runCommand;
system($runCommand);

References:

http://framework.zend.com/issues/browse/ZF-1694

http://zend-framework-community.634137.n4.nabble.com/executing-sql-file-using-zend-db-td673392.html

Tags: , , , , ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.