Problem with ZQuery

Forum related to MySQL

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
alexcagliari
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 28.04.2013, 11:49

Problem with ZQuery

Post by alexcagliari »

Hi guys,
i've a problem with zquery...
This is the code:

Code: Select all

// inizia sql
                     with ZQuery1 do
                       begin
                          Active:=False;
                          SQL.Clear;
                          SQL.Add('Insert into logbook(Callsign,Name,City) values('+Callsign.Text+','+Nome.Text+','+Country.Text+');');
                      ExecSQL;
                      end;
                     // finisce sql
And the database is in the attached image...

in Callsign (edit field) i write ABABAB
in Nome (edit field) i write CDCDCD
in Country (edit field) i write EFEFEF

The content of ZQuery.Sql.Text it's (i hope) correct:

Code: Select all

insert into logbook(Callsign,Name,City) Values(ABABAB,CDCDCD,EFEFEF);
After all Delphi say me:
SQL Error: Unknown column 'ABABAB' in 'field list

How can i resolve? :(
You do not have the required permissions to view the files attached to this post.
alexcagliari
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 28.04.2013, 11:49

Post by alexcagliari »

help me..
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1956
Joined: 17.01.2011, 14:17

Post by marsupilami »

Hello alexcagliari,

if you want to insert the data directly into the SQL then you need to encase the Strings in quotes:

Code: Select all

// inizia sql
                     with ZQuery1 do
                       begin
                          Active:=False;
                          SQL.Clear;
                          SQL.Add('Insert into logbook(Callsign,Name,City) values(' + QuotedStr(Callsign.Text) + ',' + QuotedStr(Nome.Text) + ',' + QuotedStr(Country.Text) + ');');
                          ExecSQL;
                      end;
                     // finisce sql 
but in any case it would be better to use parameters for passing the contents into the database:

Code: Select all

// inizia sql
                     with ZQuery1 do
                       begin
                          Active:=False;
                          SQL.Clear;
                          SQL.Add('Insert into logbook(Callsign, Name, City) values ( :CALLSIGN, :NOME, :COUNTRY);');
                          SQL.ParamByName('CALLSIGN').AsString := Callsign.Text;
                          SQL.ParamByName('NOME').AsString := Nome.Text;
                          SQL.ParamByName('COUNTRY').AsString := Country.Text;
                          ExecSQL;
                      end;
                     // finisce sql 
Best regards,

Jan
Daniele70
Fresh Boarder
Fresh Boarder
Posts: 6
Joined: 15.09.2023, 16:42
Location: Italia

Re: Problem with ZQuery

Post by Daniele70 »

Un altro metodo è quello di raddoppiare i singoli apici che è come usare QuotedStr

Code: Select all

                     with ZQuery1 do
                       begin
                          Active:=False;
                          SQL.Clear;
                          SQL.Add('Insert into logbook(Callsign,Name,City) values('''+Callsign.Text+''','''+Nome.Text+''','''+Country.Text+''');');
                      ExecSQL;
                      end;
Post Reply