Recent

Author Topic: [solved] MySql getting error cannot be performed on an active dataset  (Read 675 times)

JimGoodley

  • Newbie
  • Posts: 5
I spent hours reading wiki's and forum but still have problem.
Using windows 10 MySql 8.0 Lazarus 3.3.2 64 bit.
Took a copy of the procedure from main app and made a simple exe to prove the cause was not another call
to the same procedure upsetting things, still get error.
Funny thing is the main exe was working for +- 1 month then suddenly gave error, can't think of what I may have done.
I took same SQL statement (simple select) and ran in WorkBench and HeidiSQL with no problem.
In my new demo exe I changed database to world and city table, still get same error, so it's not my DB or table.
I tried with SQLQuery > options > sqoAutoCommit and error does not appear but returned dataset is empty.
I tried suggested SQLTransaction commitRetaining, CommitNone etc but error persists.
I am not a DBA or an ex Delphi programmer but Lazarus looked very interesting to me as an alternative to VS-CS.
Can someone please help me, I've got no more hairs left to tear out!!!
Jim
« Last Edit: July 31, 2024, 06:32:48 am by JimGoodley »

egsuh

  • Hero Member
  • *****
  • Posts: 1436
Re: MySql getting error cannot be performed on an active dataset
« Reply #1 on: July 28, 2024, 03:49:41 pm »
Remove SQLQuery.Execsql. For what do you need it?

JimGoodley

  • Newbie
  • Posts: 5
Re: MySql getting error cannot be performed on an active dataset
« Reply #2 on: July 28, 2024, 04:24:04 pm »
To execute the query placed in SQLQuery.text from the query variable.
From what I read so far I put the sql code in SQLQUERY then execute then loop through the dataset.
When the execute is called, I get the error in the debugger. If I continue then the dataset is empty. Obviously since the execute failed.
Regards
Jim

JimGoodley

  • Newbie
  • Posts: 5
Re: MySql getting error cannot be performed on an active dataset
« Reply #3 on: July 28, 2024, 04:44:19 pm »
I need it to execute the sql command and read from a dB table.
Regards
Jim

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: MySql getting error cannot be performed on an active dataset
« Reply #4 on: July 28, 2024, 04:46:39 pm »
Hi
@egsuh is right, loose this
Quote
      showMessage('before SQLQuery.ExecSQL');
      SQLQuery.ExecSQL;
      showMessage('after SQLQuery.ExecSQL');
I took the liberty of mod'ing your code a little bit:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.loadCities();
  2. var
  3.   query : String;
  4.   Row: integer;
  5. begin
  6.   showMessage('loadCities');
  7.   if (MySQL80Connection.Connected = False) then begin
  8.      showMessage('reconnecting');
  9.      MySQL80Connection.Connected := True;
  10.   end;
  11.   query := 'select * from city;';
  12.   clipboard.asText:= query;
  13.   try
  14.     SQLQuery.Close;
  15.     SQLQuery.SQL.Text := query;
  16.     SQLQuery.Open;
  17.     row := 0;
  18.     while not SQLQuery.eof do begin
  19.       row := row + 1;
  20.       StringGrid1.RowCount := row+1;
  21.       StringGrid1.cells[0, row] := SQLQuery.FieldByName('Name').asString;
  22.       StringGrid1.cells[1, row] := SQLQuery.FieldByName('CountryCodes').asString;
  23.       SQLQuery.next;
  24.     end;
  25.   except
  26.     ShowMessage('Error getting data from DB '+LineEnding+'query='+query);
  27.   end;
  28.   SQLTransaction.Commit; // the trx should close the query, when it's committed
  29. //  SQLQuery.Close; // <--- so this is not necessary
  30.   showMessage('end of loadShifts');
  31. end;
  32.  
I think this should compile and produce results  8)
...if the table ain't empty  :D
HTH
edit: SQLQuery.Open does exactly for reading, what you think ExecSql does.
edit2: Removed the 'showmessage' in the loop.

Regards Benny
« Last Edit: July 28, 2024, 04:51:36 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

JimGoodley

  • Newbie
  • Posts: 5
Re: MySql getting error cannot be performed on an active dataset
« Reply #5 on: July 28, 2024, 05:55:58 pm »
Thanks, I got the wrong idea of open and execute.
I will change my code tomorrow and let you know.
Regards Jim

Zvoni

  • Hero Member
  • *****
  • Posts: 2614
Re: MySql getting error cannot be performed on an active dataset
« Reply #6 on: July 28, 2024, 09:33:29 pm »
Thanks, I got the wrong idea of open and execute.
I will change my code tomorrow and let you know.
Regards Jim
Nutshell: execsql should only be used (if even then), if no result is expected
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

JimGoodley

  • Newbie
  • Posts: 5
Solved
Thanks so much guys. I must learn to read what is actually written in the wiki, not what I expect to happen.

 

TinyPortal © 2005-2018