Recent

Author Topic: SQL syntax error?  (Read 4088 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
SQL syntax error?
« on: December 06, 2014, 12:27:57 am »
Near "Notes" I'm getting a sql syntax error. My Events and NewEvents tables have only two text fields, Day and Notes. I'm trying to create a 2nd table with dates formatted y/m/d instead of y-m-d. The Select statement works fine. The error occurs with the ExecSQL statement. I'm using a SQLite db.

Here's the first insert statement
'INSERT INTO NewEvents (Day,Notes) VALUES("2004/11/26","4:00 Turkey");'

Code: [Select]
  with Query do begin       
    SQL.Clear;
    q := 'SELECT * FROM Events';
    SQL.Add(q);
    Open;
    while not EOF do begin
      r := FieldValues['Day'];
      while pos('-', r) > 0 do begin
        i := pos('-', r);
        r[i] := '/';
      end;
      n := FieldValues['Notes'];
      q := 'INSERT INTO NewEvents (Day,Notes) VALUES("' + r + '",' + n + ');';
      SQL.commatext := q;
      ExecSQL;
      Next;
    end; // while

    Conn.Transaction.Commit;
  end; // with Query
« Last Edit: December 06, 2014, 02:18:50 am by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
Re: SQL syntax error?{solved}
« Reply #1 on: December 06, 2014, 03:59:19 am »
This problem was caused by using
SQL.commatext := q;
Commas within the value of "q" resulted in more than one record being added.

A better technique is to use:
SQL.Add(q);
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

JZS

  • Full Member
  • ***
  • Posts: 194
Re: SQL syntax error?
« Reply #2 on: December 06, 2014, 07:19:23 am »
If I were you I would have changed and rethought about the entire code.

As per the case of your code, use FieldByName instead of FieldValues.
Also use SQL.Add or SQL.Text instead of CommaText:

Code: [Select]
  with Query do begin       
    SQL.Clear;
    //q := 'SELECT * FROM Events';
    //SQL.Add(q);
    SQL.Add('SELECT * FROM Events');
    Open;
    while not EOF do begin
      r := FieldByName('Day').AsString; //FieldValues['Day']; This might return multiple value if the query result in multiple records
      //while pos('-', r) > 0 do begin
      //  i := pos('-', r);
      //  r[i] := '/';
      //end;
      r:= StringReplace(r, '-', '/', [rfReplaceAll]); //no need to loop. Even better, use FormatDateTime
      //n := FieldValues['Notes'];
      q := 'INSERT INTO NewEvents (Day,Notes) VALUES("' + r + '",' + {n}FieldByName('Notes').AsString + ');';
      SQL.text := q; //why Commatext? Also could use SQL.Text:= 'INSERT INTO...' to reduce using variables unnecessarily
      ExecSQL;
      Next;
    end; // while

    Conn.Transaction.Commit;
  end; // with Query
I use recent stable release

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: SQL syntax error?
« Reply #3 on: December 06, 2014, 12:18:49 pm »
You can't use TSQLQuery twice to select and insert the same time.
Also use parameters for values parsing the insert / update query.
Code: [Select]
procedure DoSomeThing;
var SelectQuery, InsertQuery : TSQLQuery;
    TransAction : TSQLTransaction;
begin
  SelectQuery := TSQLQuery.create(nil);
  InsertQuery := TSQLQuery.create(nil);
  TransAction := TSQLTransAction.create(nil);
  try
    try
      SelectQuery.database := Conn;
      SelectQuery.Transaction := Transaction;
      SelectQuery.SQL.text := 'SELECT * FROM Events';
      InsertQuery.Transaction := Transaction;
      InsertQuery.database := Conn;
      InsertQuery.SQL.text := 'INSERT INTO NewEvents (Day,Notes) VALUES(:day, :notes)';
      Transaction.database := Conn;
      SelectQuery.Open;
      while not SelectQuery.EOF do
      begin
        r := FieldByName('Day').AsString; //FieldValues['Day']; This might return multiple value if the query result in multiple records
        r:= StringReplace(r, '-', '/', [rfReplaceAll]); //no need to loop. Even better, use FormatDateTime
        InsertQuery.params[0].value := r;
        InsertQuery.params[1].value := FieldByName('Notes').AsString; 
        InsertQuery.ExecSQL;
        SelectQuery.Next;
      end; // while
      Transaction.Commit;
    except
      Transaction.Rollback;
     end;
   finally
     SelectQuery.free;
     InsertQuery.free;
     TransAction.free;
   end;
  end
Read the DBTutorial on Wiki. It clears a lot on finding the right way to use TSQLQuery.[/code]
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018