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

查看:125
本文介绍了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' p>

'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'?

I我看了下面的问题,这是类似于我在做,但我不想在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

< a href =http://stackoverflow.com/questions/16392828/codeigniter-showing-error-no-database-selected> 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&amp; DBForge - 创建数据库和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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