Unix Technical Forum

debugging queries....

This is a discussion on debugging queries.... within the MySQL forums, part of the Database Server Software category; --> hi, how do you know what's wrong with a query when you get "You have an error in your ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:11 AM
maya
 
Posts: n/a
Default debugging queries....

hi,

how do you know what's wrong with a query when you get

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near..." etc..

I'm working in JSP/Tomcat; when a query gets ignored I type it directly
in console, but it's hard to know exactly what's causing error
sometimes.. for example on this query:

qInsert = "insert into pb set caption='" + sCaptions[i] + "' WHERE
photo=" + i + "AND photopg='" + sPhPg + "'";

which to run directly in console I converted to:

insert into pb set caption='yada yada' WHERE photo=2 AND photopg='1'";
(all values are strings except 'photo', which is a tinyint)

I get the above...

obviously I don't want to post here every time I get one of those; is
there a way (like when you get java errors it tells you what's causing
error, what syntax error is, etc..) to know what's causing error in a
MySQL query? (well, this is not a query, an 'insert'....


thank you..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:11 AM
lark
 
Posts: n/a
Default Re: debugging queries....

maya wrote:
> hi,
>
> how do you know what's wrong with a query when you get
>
> "You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near..." etc..
>
> I'm working in JSP/Tomcat; when a query gets ignored I type it directly
> in console, but it's hard to know exactly what's causing error
> sometimes.. for example on this query:
>
> qInsert = "insert into pb set caption='" + sCaptions[i] + "' WHERE
> photo=" + i + "AND photopg='" + sPhPg + "'";
>
> which to run directly in console I converted to:
>
> insert into pb set caption='yada yada' WHERE photo=2 AND photopg='1'";
> (all values are strings except 'photo', which is a tinyint)
>
> I get the above...
>
> obviously I don't want to post here every time I get one of those; is
> there a way (like when you get java errors it tells you what's causing
> error, what syntax error is, etc..) to know what's causing error in a
> MySQL query? (well, this is not a query, an 'insert'....
>
>
> thank you..

you don't need a where statement for an insert. maybe for an update but
not for an insert.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:11 AM
Kevin Day
 
Posts: n/a
Default Re: debugging queries....

> how do you know what's wrong with a query when you get
>
> "You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near..." etc..


Usually when I get that error, it means that something is seriously
wrong with the query as opposed to just a minor syntax issue. Like
lark said, don't use WHERE with INSERT. When I get that error I look
at the structure of the query itself and go back to my MySQL book to
make sure I have the correct query structure.

> qInsert = "insert into pb set caption='" + sCaptions[i] + "' WHERE
> photo=" + i + "AND photopg='" + sPhPg + "'";
>
> which to run directly in console I converted to:
>
> insert into pb set caption='yada yada' WHERE photo=2 AND photopg='1'";
> (all values are strings except 'photo', which is a tinyint)


Also, I think you're missing a space before the AND statement. I
interpret that query as:

"insert into pb set caption='yada yada' WHERE photo=2AND photopg='1'"


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:11 AM
Gordon Burditt
 
Posts: n/a
Default Re: debugging queries....

>how do you know what's wrong with a query when you get
>
> "You have an error in your SQL syntax; check the manual that
>corresponds to your MySQL server version for the right syntax to use
>near..." etc..


One of the simpler things is to display the query before running it.

>I'm working in JSP/Tomcat; when a query gets ignored I type it directly
>in console, but it's hard to know exactly what's causing error
>sometimes.. for example on this query:
>
>qInsert = "insert into pb set caption='" + sCaptions[i] + "' WHERE
>photo=" + i + "AND photopg='" + sPhPg + "'";
>
>which to run directly in console I converted to:
>
> insert into pb set caption='yada yada' WHERE photo=2 AND photopg='1'";
>(all values are strings except 'photo', which is a tinyint)


If you echo the query, you won't have to guess that the variables and
the quoting, etc. were in the real query.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com