Can I change the selected database using the Mysql2 gem?

I would like to change which database Mysql2 :: Client uses (for example, the same way as entering "use X" in the mysql command line) without having to establish a new connection to the database server.

Is it possible? How?

+3
source share
2 answers

Use select_db

db = Mysql2::Client.new
db.select_db('test')
+7
source

You can use the method queryto switch databases:

db = Mysql2::Client.new
db.query('use test')
# You're now talking to the test database
db.query('use something_else')
# You're now talking to the something_else database
+3
source

All Articles