![]() |
Switch Sqlite3 DB - $db->setDatabase - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Switch Sqlite3 DB - $db->setDatabase (/showthread.php?tid=77299) |
Switch Sqlite3 DB - $db->setDatabase - s4f3r - 08-13-2020 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) { } } [] RE: Switch Sqlite3 DB - $db->setDatabase - InsiteFX - 08-13-2020 You need to create a config like below: PHP Code: $custom = [ Fill in the Custom config above with your information. Then the line below it. If the custom config is correct it should work. RE: Switch Sqlite3 DB - $db->setDatabase - s4f3r - 08-14-2020 (08-13-2020, 10:40 AM)InsiteFX Wrote: You need to create a config like below: RE: Switch Sqlite3 DB - $db->setDatabase - captain-sensible - 08-14-2020 I have been exclusively using default so found this interesting in config/Database.php i used $custom = [ etc etc ]; but then in a controller for a quick test i used: $db = \Config\Database::connect('custom'); //$db = \Config\Database::connect($custom); gave me an error $query = $db->query('SELECT * FROM blog'); $results = $query->getResultArray(); foreach ($results as $row) { echo $row['title']; echo $row['article']; } then i found it connected and i retrieved db entries |