• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch Sqlite3 DB - $db->setDatabase

#1
Hi,
I am trying to dinamically switch a database (Sqlite3) on CI4.
Queries are working nice with the default database.
From the docs I read this:

"You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$db->setDatabase($database2_name);"


In my class I tried to set a different database before executing the query:

        $this->db->setDatabase('C:\data\test.DB');

        var_dump($this->db);// the default database isn't changed (see the object dump at the end of the post).
                                     // The query is executed on the default database
        $sql = "SELECT * FROM myTable";
        $query = $this->db->query($sql);
        $results = $query->getResultArray();


...

Am I missing something?

Thanks

Rob

================
object(CodeIgniter\Database\SQLite3\Connection)#61 (39) {
  ["DBDriver"]=>
  string(7) "SQLite3"
  ["_random_keyword":protected]=>
  array(1) {
    [0]=>
    string(8) "RANDOM()"
  }
  ["DSN":protected]=>
  string(0) ""
  ["port":protected]=>
  string(0) ""
  ["hostname":protected]=>
  string(0) ""
  ["username":protected]=>
  string(0) ""
  ["password":protected]=>
  string(0) ""
  ["database":protected]=>
  string(76) "C:\data\default.DB"
  ["subdriver":protected]=>
  NULL
  ["DBPrefix":protected]=>
  string(0) ""
  ["pConnect":protected]=>
  bool(false)
  ["DBDebug":protected]=>
  bool(true)
  ["cacheOn":protected]=>
  bool(false)
  ["cacheDir":protected]=>
  string(0) ""
  ["charset":protected]=>
  string(4) "utf8"
  ["DBCollat":protected]=>
  string(15) "utf8_general_ci"
  ["swapPre":protected]=>
  string(0) ""
  ["encrypt":protected]=>
  bool(false)
  ["compress":protected]=>
  bool(false)
  ["strictOn":protected]=>
  bool(false)
  ["failover":protected]=>
  array(0) {
  }
  ["lastQuery":protected]=>
  NULL
  ["connID"]=>
  bool(false)
  ["resultID"]=>
  bool(false)
  ["protectIdentifiers"]=>
  bool(true)
  ["reservedIdentifiers":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["escapeChar"]=>
  string(1) """
  ["likeEscapeStr"]=>
  string(13) " ESCAPE '%s' "
  ["likeEscapeChar"]=>
  string(1) "!"
  ["dataCache"]=>
  array(0) {
  }
  ["connectTime":protected]=>
  NULL
  ["connectDuration":protected]=>
  NULL
  ["pretend":protected]=>
  bool(false)
  ["transEnabled"]=>
  bool(true)
  ["transStrict"]=>
  bool(true)
  ["transDepth":protected]=>
  int(0)
  ["transStatus":protected]=>
  bool(true)
  ["transFailure":protected]=>
  bool(false)
  ["aliasedTables":protected]=>
  array(0) {
  }
}
[]
Reply


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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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