Thursday, September 02, 2010

mysql_query syntax error while sql length more than 256 chars

Source: FreePascal Community Forum

I have an error like this :

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 ''bla..bla..' at line 1.

while call my function below:

function InsertSQL(strSQL: AnsiString; CS: TCriticalSection; var ErrMsg: AnsiString): boolean;
var
  td: TDBCfg;
  SockMySQL: PMySQL;
  SQL: PChar;
begin
  CS.Acquire;

  Result := False;
  try
    {alokasi memory query}
    SQL := StrAlloc(Length(strSQL)+1);

    {Get database settings}
    td := TDbCfg.Create;
    td := GetDbConfig;

    {Make MySQL Connection}
    if CreateDbConn(td, SockMySQL, ErrMsg) then
    begin
      StrPCopy(SQL, strSQL);
      if mysql_query(SockMySQL, SQL) <> 0 then
        ErrMsg := 'InsertSQL->'+PCharToStr(mysql_error(SockMySQL))
      else
        Result := True;
    end;
  except on E:Exception do
    ErrMsg := 'InsertSQL->'+E.Message;
  end;
  td.Free;
  StrDispose(SQL);
  mysql_close(SockMySQL);

  CS.Release;
end;


The function always got this error while length of my strSQL contains more than 256 chars.
Anyone can help me to fix this function?

Thanks and best regard!
GolekUpo.Org 

-- Andi Purwito, 31 August 2010 12:46  

Answers:


You could try printing SQL before calling mysql_query. StrPCopy exists twice in Strings and SysUtils unit. Make sure you're using the one from SysUtils since the one in Strings is intended to copy shortstrings (CMIIW).
-- Mario Ray Mahardhika, 01 September 2010 16:48

Thankyou very much Roy. Absolutely fixed.
-- Andi Purwito, 02 September 2010 10:54  
-- Andi Purwito, 31 August 2010 12:46

0 Comments: