Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

Last post 05-08-2008 4:16 AM by Nithila. 5 replies.

Sort Posts:

  • Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-07-2008, 6:00 AM
    • Loading...
    • Nithila
    • Joined on 03-17-2008, 11:46 PM
    • Posts 31

    I get this error message in cmd.ExecuteNonQuery()

    "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 '//' at line 1"

    when I run the code below. The concept is to read some values from a databse and write the result into another databse if the condition is satisfied.

    Dim conString As String

    Dim myconn As MySqlConnection

    Dim sql As String

    conString = "Server=localhost; Database=xyz; Uid=root"

    myconn = New MySqlConnection(conString)

     

    sql = "Select site_id, timestring, pwr_vin, dsp_dig_in_6 from soh_data order by timestring desc limit 1"

     

    Dim mycmd As MySqlCommand = New MySqlCommand(sql, myconn)

    myconn.Open()

    Dim dr As MySqlDataReader = mycmd.ExecuteReader()

    While dr.Read()

    Response.Write("Site Id: " & dr.GetInt32(0) & "<br/>" & "Date and Time: " & dr.GetDateTime(1) & "<br/>" & "Data Logger Power:" & dr.GetDouble(2) & "<br/>" & "UPS Power Status:" & dr.GetInt32(3))

    If dr.GetDouble(2) < 20 Then

    Dim id As Int32 = dr.GetInt32(0)

    Dim time As DateTime = dr.GetDateTime(1)

    Dim dpower As Double = dr.GetDouble(2)

    Dim upower As Int32 = dr.GetInt32(3)

    Dim mycon As MySqlConnection = New MySqlConnection("Server=localhost; Database=neadb; Uid=root")

    Dim strSQL As String = "Insert into soh_event(site_id,timestring,pwr_vin,dsp_dig_in_6,status)" & _

    "values(" & _

    id & ", " & _

    "#" & time & "#" & ", " & dpower & ", " & upower & ", 'Fail')"

    Dim cmd As MySqlCommand = New MySqlCommand(strSQL, mycon)

    cmd.Connection.Open()

    cmd.ExecuteNonQuery()

    cmd.Connection.Close()

    End If

    dr.Close()

    myconn.Close()

    End While

    I am clueles about this error. Anybody encountered this kind of problem please help.

  • Re: Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-07-2008, 6:19 AM
    • Loading...
    • DigiMortal
    • Joined on 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 468
    Dunno exactly which command gives you an error but o esuggestion: Use parameterized commands instead of PHP-style SQL query strings. It helps you maintain queries more easily and to avoid SQL-injection attacks.
    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-07-2008, 6:22 AM

    Select site_id, timestring, pwr_vin, dsp_dig_in_6 from soh_data order by timestring desc limit 1

     

    run this statement art ur database then check the error.

    Thanks and Regards
    Som Nath
    Please click “Mark as Answer” on the post if it helps you,
  • Re: Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-07-2008, 8:59 PM
    • Loading...
    • Nithila
    • Joined on 03-17-2008, 11:46 PM
    • Posts 31

    Thanks for the replies.

     

    Som Nath Shukla:

    Select site_id, timestring, pwr_vin, dsp_dig_in_6 from soh_data order by timestring desc limit 1

     

    run this statement art ur database then check the error.

    I have no problem with this query. These values are retrived properly and stored in the corresponding variables like id, time  and so on. I checked these variables during debugging and they are passed correctly. It stucks when excuting the ExecuteNonQuery.

    What should be the reason?

     

  • Re: Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-07-2008, 11:55 PM
    • Loading...
    • Nithila
    • Joined on 03-17-2008, 11:46 PM
    • Posts 31

    I removed the # used to enclose the date with ' and no error message. The values written into the database correctly except the timestring which is a timestamp all zeros.

  • Re: Err: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

    05-08-2008, 4:16 AM
    Answer
    • Loading...
    • Nithila
    • Joined on 03-17-2008, 11:46 PM
    • Posts 31

    I fixed the problem. It is because of the datetime format mismatch. MySQL expects the datetime format in yyyy-mm-dd hh:mm:ss

    I added the extra code as below and the namespace.

    <%@ Import Namespace ="Microsoft.VisualBasic" %>

     

    Dim time As DateTime = dr.GetDateTime(1)

    Dim s As String 'create a string

    s = time.ToString("yyyy'-'MM'-'dd HH':'mm':'ss")

     

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter