RE: Fetch without Execute...

From: Scott V Nipp (sn4265_at_sbc.com)
Date: 10/14/03


To: Ronald J Kimball <rjk-dbi@focalex.com>
Date: Tue, 14 Oct 2003 08:35:32 -0500


        Thanks for the feedback. Your guess about 'key' being a MySQL
reserved word was correct. Now to take a stab at the next error, and do a
little reading on placeholders. Thanks again.

Scott Nipp
Phone: (214) 858-1289
E-mail: sn4265@sbc.com
Web: http:\\ldsa.sbcld.sbc.com

-----Original Message-----
From: Ronald J Kimball [mailto:rjk-dbi@focalex.com]
Sent: Tuesday, October 14, 2003 8:25 AM
To: NIPP, SCOTT V (SBCSI)
Cc: dbi-users@perl.org
Subject: Re: Fetch without Execute...

On Tue, Oct 14, 2003 at 08:06:30AM -0500, NIPP, SCOTT V (SBCSI) wrote:
> I have run into this problem before, but unfortunately, I can't seem
> to figure out what I am doing wrong again this time... I am working on a
> script to parse a data file and INSERT/UPDATE this data into a MySQL
> database table. I am able to verify that the data I am dealing with is
what
> I expect, and the simple logic in the script is performing as desired.
The
> problem I am running into at the moment is the following pair of error
> messages:
>
> DBD::mysql::st execute failed: You have an error in your SQL syntax near
> 'key ='
> 1281-02F'' at line 1 at ./emc_data2.pl line 35, <FILE> line 134.
> DBD::mysql::st fetch failed: fetch() without execute() at ./emc_data2.pl
> line 36
> , <FILE> line 134.

You're focusing on the wrong error message. The "fetch without error"
occurs because the execute failed. If you fix the execute, the fetch will
work also.

The execute fails because there's a syntax error in the SQL, as the first
message says. You should print out the constructed SQL statement and make
sure it's valid. Without seeing the entire statement, I'm not sure what
the syntax error is. Is key a reserved word in MySQL?

> while ($data = <FILE>) {
> @fields = split(/\s+/, $data);
> my $chk = $dbh->prepare("SELECT * FROM emc_usage WHERE key
> ='$fields[5]'");

Also, you should be using placeholders, or at least DBI->quote(), instead
of interpolating external data directly into an SQL statement.

Ronald



Relevant Pages

  • Re: execute/quote issue...
    ... Either use the quote() method or use placeholders, ... Preferably use placeholders, ... Some DBDs let the database parse the SQL string at the first execute, but it really makes no difference. ...
    (perl.dbi.users)
  • Re: Log DBI query and values with placeholders
    ... $sth->after an execute. ... placeholders for more secure code. ... I want to save that UPDATE query into file or database (I'll prefer ...
    (perl.dbi.users)
  • Re: Log DBI query and values with placeholders
    ... $sth->after an execute. ... placeholders for more secure code. ... I want to save that UPDATE query into file or database (I'll prefer ...
    (perl.dbi.users)
  • Re: Mail Merge fields in Form Text field
    ... > placeholders with form text fields in the merged documents. ... there's a checkbox for bookmarks. ... has to be unprotected to execute the merge. ... This reply is posted in the Newsgroup; ...
    (microsoft.public.word.mailmerge.fields)
  • Re: a simple query with date
    ... date is a reserved word in Access and should be avoided as ... >I've got a litte problem with an sql statement, ... sending to an access mdb ...
    (microsoft.public.access.queries)