CodeIgniter &DBForge - 创建数据库和表 [英] CodeIgniter & DBForge - Create Database and Tables

查看:25
本文介绍了CodeIgniter &DBForge - 创建数据库和表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 CodeIgniter 中编写一个脚本,该脚本将创建一个数据库,然后将表添加到新创建的数据库中,并将各种字段添加到新表中.

I'm trying to write a script in CodeIgniter that will create a database and then will add tables to that newly created database along with various fields in to the new table.

到目前为止,我已经使用 dbforge 来创建新数据库,但由于我使用的是数据库库并具有预设的数据库连接值,因此在创建表时,它会将其放入预先选择的数据库中而不是它刚刚创建的那个.

So far I've got dbforge to create the new database but since I'm using the database library and have pre-set database connection values, when it goes to create a table, it puts it in the pre-selected database and not the one it just created.

在database.php 配置文件中,我有以下内容:

In the database.php config file I have the following:

$db['default']['database'] = 'db1';

如果我使用下面的命令来创建一个新的数据库:

If I use the following command to create a new database:

$this->dbforge->create_database('db2');

'db2' 将被创建,但随后的命令将表放在 'db1' 中.

'db2' will get created but then the following command puts the table in 'db1'.

$this->dbforge->create_table('table1');

我需要在db2"中创建table1".如何让 CI 选择新创建的数据库('db2')在正确的位置创建表,然后切换回 'db1'?

I need 'table1' created in 'db2'. How do I get CI to select the newly created database ('db2') to create the table in the correct place, and then switch back to 'db1'?

我查看了以下与我正在做的类似的问题,但我不想在 database.php 中放置任何进一步的连接条目

I've looked at the following question which is similar to what I'm doing but I do not want to have to put any further connection entries in the database.php

Codeigniter 显示错误:未选择数据库

感谢任何帮助!

EDIT - 我应该补充一点,create_database 可以有任何名称作为数据库名称......这是一个自动创建数据库和相关表的脚本,其中数据库名称是从形式.

EDIT - I should add that the create_database could have any name for the database name... This is a script to automatically create a database and the relevant tables where the DB name is pulled from a form.

推荐答案

$this->db->query('use db2');

$this->db->query('use DB1');

这篇关于CodeIgniter &DBForge - 创建数据库和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆