doudi1750 2015-08-17 14:15
浏览 300

Laravel mysql即时创建表

I was trying to dynamicly create a database and then create table in that database

// 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("# --
", $table_schema);

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

The file table_schema.sql look like

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;
# --

But the problem is, the error kept comming out:

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; )

I tried adding this in the front, but the result is same

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

Any suggestion?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办