Welcome Guest, Not a member yet? Register   Sign In
Creating Dynamically DataBase
#1

[eluser]Ajaxian64[/eluser]
Hi all,

I'm trying to create an application on which, the first time it is launched aims to create a database.
When I start the application, I try to know where if the database is already created.

But unfortunatly, and because I think I misunderstand something I fail to my mission.
To sum up what I did
In database.php config I filled this :
Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'nice';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = FALSE;
$db['default']['stricton'] = FALSE;
in my step1 controller in the index I try to detect whether the "nice" database has already been created
Code:
function index()
    {
        $this->load->database();
        /* ******************************************** */
        /* Création des tables de la base de données */
        /* ******************************************** */
        $this->load->dbutil();
        
        // Création des tables si database non créee
        if ( ! $this->dbutil->database_exists('nice') ) {
            $this->load->dbforge();
            // In this
            $this->dbforge->create_database('nice');
        }
        // Others stuff ....
}
But the
Code:
$this->dbutil->database_exists('nice')
fails and the message is
Database config for development environment is not found. Trying global config.
and this outpu message on browser :
A Database Error Occurred

Unable to select the specified database: nice

Filename: D:\wamp\www\Idomat\system\database\DB_driver.php

Line Number: 140

Finally, I doubt about my understanding of the database_exists function.

Does someone have an explain about this behavior ?
Thanks
#2

[eluser]Gerep[/eluser]
Hi Ajaxian,

I've tried your code in my webserver and everything worked, it must be something else....try to replace your CI core files.
#3

[eluser]Ajaxian64[/eluser]
?????????????
But, Glups, I just download the last version on CI (2.0.1)

....
Anyway, thanks for your contribution
#4

[eluser]Ajaxian64[/eluser]
My code is working if, in the database.php, I do:
Code:
$db['default']['database'] = '';
instead of doing
Code:
$db['default']['database'] = 'nice';
I would be Ok for this solution but when the database 'nice' is created I would like that for every next application request then database.php with
Code:
$db['default']['database'] = 'nice';
would launch
Otherwise I don't know how to say that current database to use is 'nice'.
#5

[eluser]Gerep[/eluser]
Try something like this:

Code:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);

Based on your if, you will set a different $config to the database.
#6

[eluser]Ajaxian64[/eluser]
Thanks Gerep,
just to know : what is your CI version ?
1.7.x or 2.0.x ?

Re thanks
#7

[eluser]Gerep[/eluser]
2.0.x ?

problem solved?

and, you are welcome =)
#8

[eluser]InsiteFX[/eluser]
You can have more then one database group in the application/config/database.php
Thats what the $active_group is for to switch the active group.

Code:
$db['default']['hostname'] = "localhost";

just copy the whole default config and rename default to nice like below.
$db['nice']['hostname'] = "localhost";

Then you can use this to connect to the nice database.
Code:
$this->load->database('nice');

Hope this helps.

InsiteFX
#9

[eluser]Ajaxian64[/eluser]
Everythings work well !

Thanks all, Gerep and InsiteFX for your help
#10

[eluser]Ajaxian64[/eluser]
Hmmmm!!!!
Creation of the database is working fine but when I try to create via dbforge a table then it fails because 'Query error: No database selected'

When I perform a real sql command it works but when I try to access to the table (value filled by hand) I get the same and the same message saying that No database selected

It seems that $this->load->database('nice') doesn't work or I miss something




Theme © iAndrew 2016 - Forum software by © MyBB