• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DBForge table creation error 1046

#1
[eluser]BirdieNZ[/eluser]
I want my application to be able to re-create a database (by deleting and creating it if it already exists, or just creating it if it doesn't). I have used dbforge to drop the database and create a new one, which works correctly.

Code:
$this->dbforge->drop_database('mydatabase');
$this->dbforge->create_database('mydatabase');

Inside database.php, the default DB name is mydatabase (the same name).

However, when I add a table to the database it fails, suggesting that there is no database selected (and is unable to select the database).
Code:
$this->load->database();
$fields = array(
'category_id' => array(
        'type' => 'INT',
        'constraint' => 5,
        'unsigned' => TRUE,
        'auto_increment' => TRUE
       ),
'category_title' => array(
        'type' => 'VARCHAR',
        'constraint' => '100',
       ),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('category_id', TRUE);
$this->dbforge->create_table('category');

Why is this and what can I do to fix it?

#2
[eluser]InsiteFX[/eluser]
Why are you re-loading the database?

#3
[eluser]BirdieNZ[/eluser]
It was an attempt to make absolutely sure CI is connecting to the database before adding the tables. Removing that line doea not fix tbe problem.

#4
[eluser]InsiteFX[/eluser]
Its probably because your dropping the database so it loses its connection information.

I would do a check to see if the database exists and if not then create it and add the tables.

It could also be a timing issue between the database commands being sent.

#5
[eluser]CroNiX[/eluser]
Maybe try
Code:
$this->load->database('default');
sleep(2); //give time to connect, 2 seconds is probably more than needed...

Then try to add tables.

If that doesn't work, try $this->db->reconnect(); instead of loading the db.

However, not sure any of that will work. Never seen anyone try to drop the db they are currently connected to. There must be a better way...

#6
[eluser]BirdieNZ[/eluser]
I tried both of those, and neither worked.

@InsiteFX, I already have a check to see if the database exists before the add table code. It doesn't fail.
Code:
if ($this->dbutil->database_exists('mydatabase')) {

My reason for doing all this is so that a new user can run the database creation script when installing my software, and it will create the database and populate it with tables automatically.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.