Page 1 of 1

Problem with ZQuery

Posted: 28.04.2013, 11:59
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? :(

Posted: 30.04.2013, 14:54
by alexcagliari
help me..

Posted: 30.04.2013, 16:20
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

Re: Problem with ZQuery

Posted: 16.09.2023, 07:25
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;