in reply to Re: how to use DBI connect to >1 database
in thread how to use DBI connect to >1 database

Well done sir hippo- surprisingly, it WORKED! I used the $db1 handle which was connected to db1, and, remarkably, I can also read from tables in db2.. It seems counterintuitive since a DB handle connects to a specific database, yet it ALSO appears connected to others. It makes me wonder why I have to specify a database when making a connection?

TY

  • Comment on Re^2: how to use DBI connect to >1 database

Replies are listed 'Best First'.
Re^3: how to use DBI connect to >1 database
by ikegami (Patriarch) on Sep 28, 2023 at 17:24 UTC

    yet it ALSO appears connected to others.

    No, it doesn't. You just connect to one server, and pass it the SQL to execute.

    It makes me wonder why I have to specify a database when making a connection?

    If you don't specify the server, how would it know to which server to connect and to send the SQL?

Re^3: how to use DBI connect to >1 database
by NERDVANA (Priest) on Sep 30, 2023 at 07:40 UTC
    In some database engines you don't actually need to specify a database, since there's usually a default. But, if you did that you would need to specify fully qualified table names on every query, which would be annoying. Also, if you are a limited-access user sometimes the database wants you to choose so that it can make permission checks before allowing you to do anything.

    For a real mind-bender, Postgres has a feature where it can import views of foreign databases from other servers (or even types of server, like mysql!) and then you can run a single query across multiple database servers! It's a bit awkward to set up, though.