Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

SQL Insert with blank in the middle

by dredwerker (Initiate)
on Jul 08, 2010 at 07:47 UTC ( [id://848634]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I am trying to insert street into a database but I get an incorrect syntax error due to the space in say 'High Street' or in the case below 'something place'.
I am not sure what to do in my insert to make it work - ideas welcome :)
$dbh2->do("INSERT INTO PerlTest (Postcode,$_,streetCount) VALUES ('',$_,0)");
DBD::ODBC::db do failed: [Microsoft][ODBC SQL Server Driver][SQL Serve +r]Incorrect syntax near 'Place'. (SQL-42000) at SQL1.pl line 79.

Replies are listed 'Best First'.
Re: SQL Insert with blank in the middle
by mje (Curate) on Jul 08, 2010 at 08:03 UTC

    See quote but better would be to use a parameter:

    $dbh2->do("INSERT INTO PerlTest (Postcode,street_column,streetCount) V +ALUES ('',?,0)", undef, $mystreet);
      ALWAYS use parameters EVERYWHERE:
      $dbh2->do("INSERT INTO PerlTest (Postcode,street_column,streetCount) V +ALUES (?,?,?)", undef, '', $mystreet, 0);

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        ALWAYS use parameters EVERYWHERE
        Yeah, except of course where you can't. Note the OPs problem is having a space in the column name, with the column name stored in a variable. I do not know any engine that allows a placeholder for a column name. (In the OPs case, I wonder if the $_ for the column name is actually correct, but I do want to point out that you cannot use parameter EVERYWHERE. Table names are an other example of where you cannot use parameters.)

        For constants? Why? Why would you want to force both the server and the client to do more work?

        You should definitely use placeholders for data coming from outside, but I don't see any reason to use them for literal constants.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: SQL Insert with blank in the middle
by DrHyde (Prior) on Jul 08, 2010 at 10:05 UTC

    You have three problems.

    1. you are trying to put a value into a column of the same name. This is either an error in your code or is an error in your database design.
    2. you're not using placeholders.
    3. you've obviously not got your program to spit out the SQL that is causing the problem (or looked in the database logs). Once you've done that, it will be obvious how to make the syntax error go away.
Re: SQL Insert with blank in the middle
by cdarke (Prior) on Jul 08, 2010 at 07:55 UTC
    I think this is an SQL problem. Usually values with embedded spaces need single quotes around them, although I don't know your particular database.
      $dbh2->do("INSERT INTO PerlTest (Postcode,$_,streetCount) VALUES ('',$ +_,0)");

      I think '$_' value is 'Place' and the datatype is 'varchar'.

      While use a varchar values in 'values' block, the value must be in between single quote. try the below code.

      $dbh2->do("INSERT INTO PerlTest (Postcode,$_,streetCount) VALUES ('',' +\$_\',0)");
Re: SQL Insert with blank in the middle
by petdance (Parson) on Jul 08, 2010 at 15:49 UTC
    Please always use placeholders for parameterizing your SQL code.

    See http://bobby-tables.com/ for details on how, and refer your friends there, too.

    xoxo,
    Andy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found