Laravel mysql即时创建表

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

< pre> //创建DB
DB :: connection('mysql-group') - &gt;语句(
'CREATE DATABASE IF NOT EXISTS'。$ new_db_name。''
);
\ n //更改DB
Config :: set($ prefix。'。database',$ new_db_name);
DB :: connection('mysql-group') - &gt; setDatabaseName($ new_db_name);
DB :: connection( 'mysql-group') - &gt; reconnect();

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

//创建表
$ table_schema = file_get_contents(base_path('resources / table_schema.sql'));
$ split = explode(“# -

”,$ table_schema);

foreach($ split as $ sth){
DB :: connection('mysql-group') - &gt; select(DB :: raw($ sth));
}
</ code> </ pre>

文件table_schema。 sql看起来像</ p>

  CREATE TABLE IF NOT EXISTStable
idint(11)NOT NULL AUTO_INCREMENT,
eidint(11)NOT NULL ,
PRIMARY KEY(id),
KEYeideid
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
# -

CREATE TABLE IF NOT NOT EXISTStable2
idint(11)NOT NULL AUTO_INCREMENT,
eidint(11)NOT NULL,
PRIMARY KEY( id),
KEYeideid
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
# -

</ code> </ pre>

但是 问题是,错误一直存在:</ p>

  SQLSTATE [HY000]:一般错误:2053(SQL:CREATE TABLE IF NOT NOT EXISTStableidint(  11)NOT NULL AUTO_INCREMENT,eid int(11)NOT NULL,PRIMARY KEY(id),KEYeideid))ENGINE = InnoDB DEFAULT CHARSET = utf8;  )
</ code> </ pre>

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

  DB :: connection(  'mysql-group') - &gt;语句(
'SET SESSION wait_timeout = 65535'
);
</ code> </ pre>

有任何建议吗?</ p>
</ DIV>

展开原文

原文

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?

duanrang2627
duanrang2627 这适用于Laravel4.2。@jeremy5189您可以使用更新创建答案并接受它。我几乎错过了你的评论并回到谷歌。
接近 5 年之前 回复
dqhr76378
dqhr76378 我通过将select()更改为statement()来解决它
接近 5 年之前 回复
dtsc1684
dtsc1684 DB:连接('MySQL的组')->选择(DB::原始($某物));我不知道我是不对,但你选择(->select()),尝试查询,而不是选择
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问