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

Database Insert Depending on Data Type

by Olaf (Acolyte)
on Dec 18, 2007 at 21:37 UTC ( [id://657748]=perlquestion: print w/replies, xml ) Need Help??

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

I'm inserting data into an MSSQL database, but until the actual DBI insert I don't know the data type of the data I'm inserting.
I need to use single quotes around the data if it's text and no quotes if it's numeric. I'm going to ignore dates unless that's an easy add on.
Is there a way to find out if a variable is numeric, text, or a date?
So I'm looking at something like...
if (variable is numeric){ $sqlCmd = "Update tablename Set columnname = data where..." } else{ $sqlCmd = "Update tablename Set columname = 'data' where..." }
Just need to know what the 'variable is numeric' part ought to be.

Do you believe in miracles? Yes!

Replies are listed 'Best First'.
Re: Database Insert Depending on Data Type
by jZed (Prior) on Dec 18, 2007 at 21:47 UTC
    No, please don't do that. Either use the $dbh->quote() method, or even better use placeholders. Use placeholders! Did I mention? Use placeholders - more secure, more portable, the right way to do it. Both quote() and placeholders automatically quote strings and leave numbers unquoted.
    my $sth = $dbh->prepare(" UPDATE tablename SET columname = ? "); $sth->execute($data);
      Hmmmm...this seems to be eluding me I have your suggestions in my code below:
      #---SQL to change the column data for ($count = 0; $count < $EasySetDataCount; $count++) { #------Put quotes around data if needed $QuotedNewEasySetDataName = $dbh->quote($NewEasySetDataNames[$count]); $QuotedEasySetDataName = $dbh->quote($EasySetDataNames[$count]); #------Create SQL statement $sqlStatement = "UPDATE ".$ChosenEasySetTableName." SET ".$ChosenEasyS +etColumnName." = ".$NewEasySetDataNames[$count]." WHERE ".$ChosenEasy +SetColumnName." = ".$EasySetDataNames[$count]; #------print it out for debugging print $sqlStatement; #------prepare and execute $sqlCmd = $dbh->prepare( $sqlStatement ); $sqlCmd->execute() or die "SQL Error: $DBI::errstr\n"; }
      And the run results...
      Uncaught exception from user code: SQL Error: [Microsoft][ODBC SQL Server Driver][SQL Server]Inva +lid column name 'Other'. (SQL-42S22) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not +be prepared. (SQL-42000)(DBD: st_execute/SQLExecute err=-1) at C:\temp\EasySetRenameTool.pl line 155 Issuing rollback() for database handle being DESTROY'd without explici +t disconnect() at C:\temp\EasySetRenameTool.pl line 155, <STDIN> line + 5. UPDATE AI_ESP SET Race = red WHERE Race = Other
      Note the lack of quotes in the Printed SQL at the end around RED and OTHER.
      As I've only submitted a code snippet, line 155 refers to the $sqlCmd->execute(); line
      If I manually put single quotes into the SQL it works. Or if I leave the quotes off and the dataset is numeric it works.
      Do you believe in miracles? Yes!
        It looks like you are not using the same variables in the SQL that you used the quote() method on. I really advise you to use placeholders instead. Something like:
        my $sqlCmd = $dbh->prepare(" UPDATE $table SET $column = ? WHERE $column = ? "); for ($count = 0; $count < $EasySetDataCount; $count++) { $sqlCmd->execute( $NewEasySetDataNames[$count], $EasySetDataNames[$count], ); }
        The placeholders mean you don't need to worry about quoting at all, you just say where the value should go (with the question marks in the SQL) and what data should be placed in those places (the items in the call to execute).
Re: Database Insert Depending on Data Type
by moritz (Cardinal) on Dec 18, 2007 at 21:44 UTC
    You can use placeholders, and let DBI do the work for you:
    my $st = $dbh->prepare("Update tablename set columname = ?"); $st->execute($data);

    There are other good reasons for placeholders, like security and speed.

Re: Database Insert Depending on Data Type
by chrism01 (Friar) on Dec 19, 2007 at 01:08 UTC
    If you know the column types, then you know what the data types should be. As mentioned, using quotes on nums is fine anyway. The DB ought to reject invalid types anyway.
Re: Database Insert Depending on Data Type
by Cop (Initiate) on Dec 19, 2007 at 00:45 UTC

    As others have said, you should use placeholder. On the other hand, you shouldn't care regardless... If the column takes string 'abc', it will also take '123'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found