Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Solved: DBI and MySQL not setting database properly

by KolassaRaptor (Monk)
on Feb 24, 2016 at 16:48 UTC ( [id://1156034]=perlquestion: print w/replies, xml ) Need Help??

KolassaRaptor has asked for the wisdom of the Perl Monks concerning the following question:

My fellow Perl Monks,

I've come across an issue with using DBI/DBD::mysql when connecting to a remote MySQL database.

I can achieve the connection and query it just fine, however it seems that when I connect to a remote host I am required to specify the tables in my statements in the 'database.table' format instead of being able to shorthand it to just 'table' like all the documentation would lead me to believe was possible (per examples in DBD::mysql).

As an example of my connection code...

my $dsn = "dbi:$driver:database=$database;host=$host;port=$port"; my $dbh = DBI->connect( $dsn, $cfg->{username}, $cfg->{password}, { AutoCommit => 1, RaiseError => 0, PrintError => 0, PrintWarn => 0, }, );

Again, I am able to connect and query the database just fine, but if I do not prepend the database name to the table in my SQL statements, it will return an error.

An example SQL statement...

SELECT * FROM group

The database will return the following...

You have an error in your SQL syntax; check the manual that correspo +nds to your MySQL server version for the right syntax to use near 'gr +oup' at line 2

All I have to change to make it work is prepending "$database." to my table 'group'.

If I check the dbh attribute Name, it will return the entire dsn minus the 'dbi:mysql:' at the front of it.

I have even tried doing a $dbh->do("USE $database;") on the connection, but it still requires me to prepend the database name to my table names in my SQL statements.

Is this expected behavior?

Replies are listed 'Best First'.
Re: DBI and MySQL not setting database properly
by Corion (Patriarch) on Feb 24, 2016 at 17:14 UTC

    Does your statement work in the MySQL command line client?

    I suspect your problem occurs because group is a reserved word in SQL. Maybe it works if you use the following form of the statement:

    SELECT * FROM "group"

      You are absolutely right, it appears to be because of using a reserved word as my table name. I was able to query other table names that did not clash with reserved words just fine. The 'group' table just happened to be the first one that I had added to the database and thus the one that made me start prepending the database name.

      Well, chalk that one up to a learning experience. Thanks!

      Just watch out, as quoting preserves case, which is not usually the case for unquoted names (which are usually uppercased).

Re: DBI and MySQL not setting database properly
by KolassaRaptor (Monk) on Feb 24, 2016 at 17:12 UTC

    As a random curiosity, I tried encapsulating the bare table name inside of tick marks (`s), and that appears to allow it to work without having to prepend the database name to the table.

    To me, this is odd behavior, as I can use a bare database.table without tick marks just fine. Why would it requires the table name on it's own to be surrounded by tick marks when all the documentation would lead someone to believe otherwise?

    In case it's relevant, I'm connecting to a MySQL 5.5 server.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-26 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found