Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Eval error on connectd DBI handle

by MidLifeXis (Monsignor)
on Feb 23, 2016 at 21:43 UTC ( [id://1155963]=note: print w/replies, xml ) Need Help??


in reply to Eval error on connectd DBI handle

Multiple items:

  • I see no reason given the code provided, that this has to be done via eval. Do a $dbh->do($generated_table_data). I would recommend using the quoting or placeholder functions, if at all possible, of the dbi to avoid Bobby Tables issues.
  • You are stringifying the reference for the $dbh, which is where the DBI::db=HASH... stuff is coming from. Don't do that. As I said in the last point, eval is not necessary here.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: Eval error on connectd DBI handle
by kennethk (Abbot) on Feb 23, 2016 at 22:02 UTC
    Note that quoting or placeholder functions will not work for database identifiers like table names, and only for content. So code like:
    my $query = $dbh->prepare('CREATE TABLE ?'); $query->execute('TABLE_NAME');
    will fail because the database engine will render that as
    CREATE TABLE 'TABLE_NAME'
    which is nonsense syntax.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Note that quoting or placeholder functions will not work for database identifiers like table names

      DBI has a quote_identifier() method that handles all required quoting of database identifiers. Unfortunately, you can't use placeholders for identifiers, so you have to call quote_identifier() manually, like this:

      my $cmd='create table '.$dbh->quote_identifier($tablename); $dbh->do($cmd);

      And by the way: There is also a quote() method in DBI. For all but some very exotic cases, just forget that it exists and use placeholders. I think quote() should die or at least warn when being called from non-DBI, non-DBD code. It is usually just wrong to use it instead of using placeholders. See Re: Counting rows Sqlite, Re^2: Massive Memory Leak, Re^3: Variable interpolation in a file to be read in, Re^5: Variable interpolation in a file to be read in for more details.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Eval error on connectd DBI handle
by beartham (Novice) on Feb 23, 2016 at 21:50 UTC
    Thanks so much for the alternative approaches. I'll pick one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found