Laravel mysql即时创建表

时间:2015-08-17 14:15:00

标签: php mysql laravel

我试图动态创建数据库,然后在该数据库中创建表

// Create DB
DB::connection('mysql-group')->statement(
    'CREATE DATABASE IF NOT EXISTS `' . $new_db_name . '`'
);

// Change DB
Config::set($prefix . '.database', $new_db_name);
DB::connection('mysql-group')->setDatabaseName($new_db_name);
DB::connection('mysql-group')->reconnect();

Log::debug(DB::connection('mysql-group')->getDatabaseName());

// Create Table
$table_schema = file_get_contents(base_path('resources/table_schema.sql'));
$split = explode("# --\n", $table_schema);

foreach( $split as $sth ) {
    DB::connection('mysql-group')->select(DB::raw($sth));
}

文件table_schema.sql看起来像

CREATE TABLE IF NOT EXISTS `table`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `eid` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `eid` (`eid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# --
CREATE TABLE IF NOT EXISTS `table2`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `eid` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `eid` (`eid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# --

但问题是,错误一直在发生:

SQLSTATE[HY000]: General error: 2053 (SQL: CREATE TABLE IF NOT EXISTS `table`  (`id` int(11) NOT NULL AUTO_INCREMENT, `eid` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `eid` (`eid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; )

我尝试在前面添加它,但结果是相同的

DB::connection('mysql-group')->statement(
    'SET SESSION wait_timeout=65535'
);

有什么建议吗?

0 个答案:

没有答案