Welcome Guest, Not a member yet? Register   Sign In
Automatically create database
#1

[eluser]Aquillyne[/eluser]
is there a way to have CI automatically create the database specified in your standard config file? Currently, CI will deliver an error on anything to do with your db until you manually create it as it's trying to select what isn't there. However I noticed in the CI code that a routine already exists to autocreate the missing database when it can't be selected and that's exactly what I want to do! How can I turn on this functionality? I've tried hacking it for quite a while now with no luck. There must be a simple way? Or can someone suggest a working hack? Thanks.
#2

[eluser]xwero[/eluser]
the db error only happens if you load a db related file. If you don't need a db CI is not forcing you to use one. Another thing to keep in mind is that you could use a less privileged account to connect with the db out of security precaution.

Could you tell me where you found the possibility to autocreate a db?

Most frameworks have a generator to create the tables the frameworks needs to operate but AFAIK CI only needs a session table and i believe that code is still in SVN.

To make it easier for yourself you can upload phpminiadmin with CI to create the database and tables. Or you could create a generator and run that before you want to access the database/tables in CI.
#3

[eluser]Aquillyne[/eluser]
No no. I WANT the database. I want CI to autocreate it when I access it for the first time. I don't want to have to manually set it up. You mentioned making a generator. How can I do that. The irony is that to activate the database class at all, you have to select your database! I can't create it without activating the database class, and I can't activate the class without creating it!
#4

[eluser]drewbee[/eluser]
Why are you trying to query a non-existant database?

I am not exactly sure where your headed with this one, but I would never want any of my code generating a database if its not found -- thats just uneeded additional over head.
#5

[eluser]Aquillyne[/eluser]
I'm making a website that needs to be entirely self contained, run out of the box. My client doesn't know how to manually create a database. Therefore I want the code to just generate the database automatically the first time it is run.
#6

[eluser]drewbee[/eluser]
Oh ok. What I may suggest is to not call this database code during script execution, but rather create an install script. This script would then need to be deleted once installation is complete.

All your user would need to do is upload all the files, run the install file by visiting example.com/install.php.

I would recomend keeping the install file seperate from the general MVC approach as it will cut down on confusion (and you may be able to keep it within 1 file);

Install script:
:: Install database
- Check for database existance
- If not exist create
- If exists suggest option to purge data and re-create
#7

[eluser]Derek Allard[/eluser]
Have you read through Database Forge Class?
#8

[eluser]Aquillyne[/eluser]
Thanks for the great help! I can't use the dbforge class derek because to instantiate it I need to load the database class. As soon as you load the db class CI selects the nonexistent database and you get an error. Drewbee your idea is better but how can I check whether the database exists from code? Most of the functions to do that require the database class and then we have the same old problem! I could hard code it but that has no portability and far too hacky. I wanted to extend the database class to solve this but I don't think you can!
#9

[eluser]drewbee[/eluser]
You are correct in that it cannot be extended.

You have yourself in a nice bind on this one. As I previously stated though, since the install.php will not be using code igniters framework, I would vouch for a custom build up. In that one file during database initialization create a statement that accepts input from a user as to what database they are using, and run the create accordingly.


mysql_db_name

Will get that, as well as mysql_list_tables.

There are all kinds of goodies in interacting with the database, and if really needed you could pick apart the different drivers and see how the forge interacts with it to handle each database system.

mysql functions
#10

[eluser]Aquillyne[/eluser]
Any way I can force CI not to deliver a fatal error if it can't select db? Can I catch that error?




Theme © iAndrew 2016 - Forum software by © MyBB