简体   繁体   English

CodeIgniter:使用数据库伪造测试数据库是否存在

[英]CodeIgniter : use Database Forge to test if DB exists

I'm using CodeIgniter 3.1.0 to develop an app. 我正在使用CodeIgniter 3.1.0开发应用程序。 In order to improve its installation, I've written an Install_Controller, and an Install_Model. 为了改进其安装,我编写了一个Install_Controller和一个Install_Model。 I'm using Database Forge class to manage the database. 我正在使用Database Forge类来管理数据库。 Thanks to it, I can create a DB, but I can't check before if it exists. 多亏了它,我可以创建一个数据库,但是无法检查它是否存在。 Actually, my idea was to pass a query to dbforge, like "CREATE DATABASE IF NOT EXISTS mydatabase". 实际上,我的想法是将查询传递给dbforge,例如“如果不存在mydatabase,则创建数据库”。 I've search the doc but there wasn't any function that could meet my requirements. 我已经搜索过该文档,但是没有任何功能可以满足我的要求。 Where can I write such a query ? 我在哪里可以写这样的查询?

Here is an excerpt from the former : 这是前者的摘录:

$this->load->model('Install_Model');
$this->Install_Model->launchInstall();

Here is the code of the latter : 这是后者的代码:

class Install_Model extends CI_Model {

    public function __construct()   {

        parent::__construct();

            $this->load->dbforge();

    }

    public function index() {

        if ($this->dbforge->create_database('mydatabase')) 
        {
            echo "OK";
        }

        else {

            echo "Error somewhere";
        }

        $this->load->database('mydatabase');
    }

}

you can use this custom SQL query function within your installation script to create new database. 您可以在安装脚本中使用此自定义SQL查询功能来创建新数据库。

public function createDB($databse) {
    $strSQL = "CREATE DATABASE IF NOT EXISTS $databse";
    $query = $this->db->query($strSQL);
   if (!$query) {
      throw new Exception($this->db->_error_message(),
      $this->db->_error_number());
                return FALSE;
            } else {
                return TRUE;
            }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM