Welcome Guest, Not a member yet? Register   Sign In
Switch Sqlite3 DB - $db->setDatabase
#3

(08-13-2020, 10:40 AM)InsiteFX Wrote: You need to create a config like below:

PHP Code:
$custom = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];

$db = \Config\Database::connect($custom); 

Fill in the Custom config above with your information.

Then the line below it. If the custom config is correct it should work.

======================

Thanks for your help.
Not sure if I understood well.
I am able to switch the connection of the database, this works fine:

PHP Code:
private function selectDB() {
    $dbId = $this->session->get('dbId');
    $this->db = \Config\Database::connect($dbId);


however I still don't understand how to use the
PHP Code:
$db->setDatabase($database2_name); 

method suggested in the documentation.

All the other parameters in the config are the same, the only difference is the name of the DB (SQLite3).
The idea behind that is that I would like to avoid creating a new DB connection if I am able to switch just the DB file (not sure if it is possible).

Thanks

R.

PHP Code:
  public $default = [
    'DSN'      => '',
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => APPPATH 'Data\DB_TEST1.DB',
    'DBDriver' => 'SQLite3',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => '',
  ];

  public $db2 = [
    'DSN'      => '',
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => APPPATH 'Data\DB_TEST2.DB',
    'DBDriver' => 'SQLite3',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => '',
  ]; 
Reply


Messages In This Thread
Switch Sqlite3 DB - $db->setDatabase - by s4f3r - 08-13-2020, 05:31 AM
RE: Switch Sqlite3 DB - $db->setDatabase - by s4f3r - 08-14-2020, 01:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB