Recent

Author Topic: Problems in TSQLQuery  (Read 12897 times)

workinghard

  • Jr. Member
  • **
  • Posts: 74
Problems in TSQLQuery
« on: March 27, 2012, 10:07:10 am »
Recently i've been working on calculating the land maintenance rate for the area where I live, and these are the line so far.
These are the codes

Code: [Select]
procedure TForm5.Button1Click(Sender: TObject);

  begin
  with SQLQuery1 do
  begin
    close;
    sql.clear;
    sql.add('INSERT INTO trans(Ad,date,total)VALUES((SELECT M.address FROM master AS M WHERE M.address = edAddress),CURRENT_TIMESTAMP,(SELECT((M.land*RM.landrate)+(M.home*RM.buildingrate))FROM Master AS M,maintenance AS RM WHERE M.Cluster = RM.Cluster AND M.Address=edAddress)');
    params.parambyname('Alamat').AsString := Alamat.text;
    open;
  end;
     

But, I get this error
SQLQuery1: Parameter "Address" not found

What does that mean?
The query code ( i know it's more than 255 lines, but i've shortened it already on the program )
Is the code wrong? or is there something else?

I had an edit box for address, and a tdbgridbox, and a calculate button in the form, and that's all (Although i will add it later if i can figure this out)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Problems in TSQLQuery
« Reply #1 on: March 27, 2012, 10:37:49 am »
Quote
Code: [Select]
    sql.add('INSERT INTO trans(Ad,date,total)VALUES((SELECT M.address FROM master AS M WHERE M.address = edAddress),CURRENT_TIMESTAMP,(SELECT((M.land*RM.landrate)+(M.home*RM.buildingrate))FROM Master AS M,maintenance AS RM WHERE M.Cluster = RM.Cluster AND M.Address=edAddress)');
    params.parambyname('Alamat').AsString := Alamat.text;
That looks like a copy and paste and cross fingers in the hope it works.
1) you specify a parameter 'Alamat' but there is no :Alamat in the query
2) I assume edAddress is the address you get in the editbox. Then use it as a parameter in you query by entering it as :Address  and
Code: [Select]
params.parambyname('Address').AsString := edAddress.text3) The query itself is a disaster.  Rewrite it as
Code: [Select]
INSERT INTO trans (Ad,date,total)
SELECT M.address, CURRENT_TIMESTAMP, (M.land*RM.landrate)+(M.home*RM.buildingrate)
FROM Master AS M,maintenance AS RM
WHERE M.Cluster = RM.Cluster AND M.Address=:Address
So much easier to read, isn't it? For the syntax look here http://dev.mysql.com/doc/refman/5.1/en/insert-select.html.

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #2 on: March 27, 2012, 11:25:45 am »
I'm really sorry, I've forgot to translate the word :"Alamat" to english
in the params, actually it should be address as well
still not working, even after i've fiddled with it

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problems in TSQLQuery
« Reply #3 on: March 27, 2012, 12:04:08 pm »
Don't let us guess, post the adjusted code ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #4 on: March 27, 2012, 12:13:10 pm »
Code: [Select]
close;

sql.clear;

sql.add ('INSERT INTO trans (Ad,date,total)
SELECT M.address, CURRENT_TIMESTAMP, (M.land*RM.landrate)+(M.home*RM.buildingrate)
FROM Master AS M,maintenance AS RM
WHERE M.Cluster = RM.Cluster AND M.Address=:Address');

params.parambyname('Address').AsString := edAddress.text;
open;

Here's the code...

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Problems in TSQLQuery
« Reply #5 on: March 27, 2012, 12:14:18 pm »
You should not use "open", but "execSQL";
1.0/2.6.0  XP SP3 & OS X 10.6.8

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #6 on: March 27, 2012, 12:21:42 pm »
i've tried it, still raised the same error message:  "SQLQuery1: Parameter "Address" not found"

also, on the master table, the M.address value, M.land, M.building, M.Cluster and on the maintenance table, the RM.landrate, RM.buildingrate existed and not null

or maybe should i rewrite the code again and see what will happen?
« Last Edit: March 27, 2012, 12:24:53 pm by workinghard »

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #7 on: March 27, 2012, 12:55:02 pm »
i've tried, but then it won't compile, saying that identifier idents no member "params"

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Problems in TSQLQuery
« Reply #8 on: March 27, 2012, 01:20:24 pm »
The complete code should read:
Code: [Select]
  begin
  with SQLQuery1 do
  begin
    close;
    sql.clear;
    sql.add ('INSERT INTO trans (Ad,date,total) SELECT M.address, CURRENT_TIMESTAMP, (M.land*RM.landrate)+ M.home*RM.buildingrate) FROM Master AS M,maintenance AS RM WHERE M.Cluster = RM.Cluster AND M.Address=:Address');
    parambyname('Address').AsString := edAddress.text;
    ExecSQL;
  end;

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #9 on: March 27, 2012, 01:39:10 pm »
I've also tried doing it ludob, but still, it can't work
In any ways, it couldn't work...
could it be that lazarus has errors on this part? (I'm using 0.9.30 with fpc 2.4.2)
should i recompile?
 :'(
« Last Edit: March 27, 2012, 01:47:45 pm by workinghard »

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Problems in TSQLQuery
« Reply #10 on: March 27, 2012, 02:04:09 pm »
A lot of changes have been made to sqldb since fpc 2.4.2. I suggest you upgrade to lazarus 0.9.30.4 that comes with fpc 2.6.0. You can find it here http://sourceforge.net/projects/lazarus/files/
 

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #11 on: March 27, 2012, 04:08:57 pm »
Even after updating, it still won't solve the problems....
what could possibly be wrong in the code? :o

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Problems in TSQLQuery
« Reply #12 on: March 27, 2012, 04:48:35 pm »
Have you tried without the "with"  (just clutching at straws, but you never know)?
Code: [Select]
SQLQuery1.close;
SQLQuery1.sql.clear;
SQLQuery1.sql.add ('INSERT INTO trans (Ad,date,total) SELECT M.address, CURRENT_TIMESTAMP, (M.land*RM.landrate)+ M.home*RM.buildingrate) FROM Master AS M,maintenance AS RM WHERE M.Cluster = RM.Cluster AND M.Address=:Address');
SQLQuery1.params.parambyname('Address').AsString := edAddress.text;
SQLQuery1.ExecSQL;
« Last Edit: March 27, 2012, 04:51:48 pm by Arbee »
1.0/2.6.0  XP SP3 & OS X 10.6.8

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Problems in TSQLQuery
« Reply #13 on: March 27, 2012, 04:58:51 pm »
And if you can post the entire porgram or the entire unit, that might help, too...

What error message are you seeing on compilation? Still blabla idents no params or similar?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

workinghard

  • Jr. Member
  • **
  • Posts: 74
Re: Problems in TSQLQuery
« Reply #14 on: March 27, 2012, 05:10:30 pm »
yes, and even worse
i've updated the lazarus to the most recent version, and I started to get this error )in the attachment

Code for the main form
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, unit3, unit4, unit6;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { private declarations }
 end;
var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form3 := TForm3.Create(nil);
  Form3.ShowModal;
  Form3.Release;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  application.terminate
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Form4 := TForm4.Create(nil);
  Form4.ShowModal;
  Form4.Release;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Form6 := TForm6.create(nil);
  Form6.showmodal;
  form6.release;
end;

end.

Code for the second form
Code: [Select]
unit Unit3;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, mysql50conn, sqldb, db, dbf, FileUtil, Forms, Controls,
  Graphics, Dialogs, DbCtrls, DBGrids, ExtCtrls, StdCtrls;

type

  { TForm3 }

  TForm3 = class(TForm)
    Insert: TButton;
    cancel: TButton;
    ComboCluster: TComboBox;
    Datasource1: TDatasource;
    address: TLabeledEdit;
    Cluster: TLabel;
    lastrecordedwatercounter: TLabeledEdit;
    buildingsize: TLabeledEdit;
    landsize: TLabeledEdit;
    Name: TLabeledEdit;
    MySQL50Connection1: TMySQL50Connection;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
    procedure cancelClick(Sender: TObject);
    procedure InsertClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.lfm}

{ TForm3 }

procedure TForm3.InsertClick(Sender: TObject);
begin
  SQLQuery1.insert;
  sqlquery1.fieldbyname('Name').Text:= Name.Text;
  sqlquery1.fieldbyname('address').Text:= Address.Text;
  SQLQuery1.Fieldbyname('cluster').text:= combocluster.text;
  sqlquery1.fieldbyname('Landsize').Text:= Landsize.Text;
  sqlquery1.fieldbyname('buildingsize').Text:= buildingsize.Text;
  sqlquery1.fieldbyname('lstrcrdwatercounter').Text:= lastrecordedwatercounter.Text;
  sqlquery1.post;
  sqlquery1.applyupdates;
  sqlquery1.refresh;

  showmessage('new user has been made');
  form3.close;
end;

procedure TForm3.cancelClick(Sender: TObject);
begin
  Form3.close;
end;


end.

Code for the 3rd form:
Code: [Select]
unit Unit5;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, mysql50conn, sqldb, DB, FileUtil, Forms, Controls,
  Graphics, Dialogs, StdCtrls, DBGrids;

type

  { TForm5 }

  TForm5 = class(TForm)
    edAlamat: TEdit;
    Button1: TButton;
    PemakaianAirTerakhir: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    MySQL50Connection1: TMySQL50Connection;
    SQLQuery1: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form5: TForm5;

implementation

{$R *.lfm}

{ TForm5 }

procedure TForm5.Button1Click(Sender: TObject);

  begin
  with SQLQuery1 do
  begin
    close;
    sql.clear;
    sql.add('INSERT INTO trans(Ad,date,total)VALUES((SELECT M.address FROM master AS M WHERE M.address = edAddress),CURRENT_TIMESTAMP,(SELECT((M.land*RM.landrate)+(M.home*RM.buildingrate))FROM Master AS M,maintenance AS RM WHERE M.Cluster = RM.Cluster AND M.Address=edAddress)');
    params.parambyname('Address').AsString := Address.text;
    open;
  end;

end;
end.


those are the codes, so far
but the new error makes everything fail...
i can't compile anything, and i'm stuck on the edit only, cannot run tests anymore

 

TinyPortal © 2005-2018