Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

changing database with DBI and DBD-ODBC

by BeneSphinx (Acolyte)
on Sep 24, 2012 at 19:48 UTC ( #995434=perlquestion: print w/ replies, xml ) Need Help??
BeneSphinx has asked for the wisdom of the Perl Monks concerning the following question:

I have a DSN that correctly works with DBI and DBD::ODBC to connect to a MSSQL server from a Windows machine. However, I can't figure out how to use a database other than the DSN's default database.

Whenever I try specifying the database in the ODBC connection string, it seems to invalidate the whole string ("Data source name not found and no default driver specified"). The DSN that does work for me is simpler, just "dbi:ODBC:mydsn", but this uses the default database.

I'm wondering if you know any way to specify a different database, either at the time of connection (in the connection string), or later with a command. Nothing in the docs seems to match this. Thanks in advance for your help!

Comment on changing database with DBI and DBD-ODBC
Re: changing database with DBI and DBD-ODBC
by roboticus (Canon) on Sep 24, 2012 at 19:52 UTC

    BeneSphinx:

    I generally do this:

    $DB->do("use new_database");

    after the connection is successful to move to the other database.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      roboticus: That worked for me. Thanks! I tried this earlier and for some reason it didn't work for me, but I must not have done it properly. It now works for me both with do() and also with prepare()/execute() (my favorite approach).
Re: changing database with DBI and DBD-ODBC
by mje (Deacon) on Sep 24, 2012 at 20:25 UTC

    roboticus is correct as far as changing the database after you have connected. However another way is to use a DSN-less connection where you specify the database at connect time. For this you use a connect string like "DRIVER={SQL Server};Database=mydatabase;UID=username;PWD=password;" where "SQL Server" is the ODBC driver named shown in the ODBC Administrator drivers tab. This way you don't have to create a DSN beforehand and can connect to any database at connect time. If you search for SQL Server connection strings you'll find other attributes MS SQL Server can take.

Re: changing database with DBI and DBD-ODBC
by runrig (Abbot) on Sep 24, 2012 at 20:27 UTC
    Another way is to create a 'File DSN' as a starting template. Join all the lines with ";" instead of newlines and use that as the DBI DSN. Find where the database is specified in the string, and change that as desired.

      As I'm sure runrig knows there are 3 basic ways to connect via ODBC. DSN=yourdsn, FILEDSN=a_file_with_Attributes and so called DSN-less connection with DRIVER=xxx. On Windows you can also add odbc_driver_complete which may throw a dialogue (if you have a window handle) to complete the connection attributes via a dialogue (you might use this to prompt for username/password etc)

      One really useful thing is odbc_out_connect_string which you can use after any successful connect to find out the connection string you can use to connect to this data source again in the future. This is typically what MS Access (and other apps) use to store a connection string after you've filled in a dialog to connect to an ODBC driver.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995434]
Approved by mje
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (15)
As of 2014-07-11 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls