The Votes column defualt value is 0. Every time when I click the button it must add whichever value I want to the specific row I want.
My error is :
Syntax error : Update statement. [[Delphi]]
This is my code :
procedure TForm4.BitBtn1Click(Sender: TObject);
var
spinval : integer;
begin
spinval := SpinEdit1.value;``
// Candidatetable.Insert;
// Candidatetable['Votes'] := Candidatetable['Votes'] + spinval;
ADOQuery1.Active := false;
ADOQuery1.SQL.Text := 'Update Candidate_table set votes = ''' +
Candidatetable['Votes'] + IntToStr(spinval) +
''' where Name = ''' + DBLookupComboBox1.Text + '''';
ADOQuery1.ExecSQL;
ADOQuery1.Active := false;
ADOQuery1.SQL.Text := 'Select * from Candidate_table';
ADOQuery1.Active := true;
MessageDlgPos('Thank you for voting. You will be logged out.' , mtInformation, [mbOK], 0, 1000, 500);
Form4.Hide;
Form2.Show;
end;
PlEASE HELP =)
Thanks.
I think this is what you are looking for.
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('Update Candidate_table');
ADOQuery1.SQL.Add('set votes = votes + :Votes');
ADOQuery1.SQL.Add('where Name = :Name');
ADOQuery1.Parameters[0].Value := spinval;
ADOQuery1.Parameters[1].Value := DBLookupComboBox1.Text;
Related
I want to store images in a database using sql but cant seem to get it to work:
qry.SQL.Clear;
qry.Sql.Add('update tbl set pic = :blobVal where id = :idVal');
qry.Parameters.ParamByName('idVal')._?:=1;
.Parameters has no .asinteger like .Param has but .Param isn't compatible with a TADOquery - to workaround I tried:
a_TParameter:=qry.Parameters.CreateParameter('blobval',ftBlob,pdinput,SizeOf(TBlobField),Null);
a_TParam.Assign(a_TParameter);
a_TParam.asblob:=a_Tblob;
qry.ExecSql;
This also doesnt work:
qry.SQL.Clear;
qry.Sql.Add('update tbl set pic = :blobVal where id = 1')
qry.Parameters.ParamByName('blobVal').LoadFromStream(img as a_TFileStream,ftGraphic);//ftblob
//or
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg',ftgrafic);//ftblob
qry.ExecSql;
Should be something like:
qry.Parameters.Clear;
qry.Parameters.AddParameter.Name := 'blobVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
// or load from stream:
// qry.Parameters.ParamByName('blobVal').LoadFromStream(MyStream, ftBlob);
qry.Parameters.AddParameter.Name := 'idVal';
qry.Parameters.ParamByName('idVal').Value := 1;
qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.ExecSQL;
To read the BLOB back from the DB:
qry.SQL.Text := 'select id, pic from tbl where id = 1';
qry.Open;
TBlobField(qry.FieldByName('pic')).SaveToFile('c:\sample_2.jpg');
I'm using Lazarus, not Delphi, but I guess its usually the same syntax. If so, here's a slight improvement on kobiks suggestion:
Parameters are added automatically if the SQL.Text is assigned before trying to assign values to the parameters. Like this:
qry.Parameters.Clear;
qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
qry.Parameters.ParamByName('idVal').Value := 1;
qry.ExecSQL;
I wrote this as an answer to this q,
Delphi save packed record as blob in a sql database
which is currently flagged as a duplicate, possibly incorrectly because the technique
used by the OP as described in comments appears to be correct. So, the cause of the problem may lie elsewhere.
If the Duplicate flag gets removed, I'll re-post this answer there.
The following code works fine for me against a Sql Server table defined as shown below.
The data from Rec1 is saved into the table and correctly read back into Rec2.
(* MS Sql Server DDL
CREATE TABLE [blobs] (
[id] [int] NOT NULL ,
[blob] [image] NULL ,
CONSTRAINT [PK_blobs] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
*)
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
qBlobInsert: TADOQuery;
qBlobRead: TADOQuery;
Button1: TButton;
procedure Button1Click(Sender: TObject);
[...]
type
TMyRecord = packed record
FontName: string[30];
FontSize: word;
FontColor: integer;
FontStyle: word;
Attachement: string[255];
URL: string[255];
end;
const
scInsert = 'insert into blobs(id, blob) values(:id, :blob)';
scSelect = 'select * from blobs where id = %d';
procedure TForm1.Button1Click(Sender: TObject);
begin
TestInsert;
end;
procedure TForm1.TestInsert;
var
Rec1,
Rec2 : TMyRecord;
MS : TMemoryStream;
begin
FillChar(Rec1, SizeOf(Rec1), #0);
FillChar(Rec2, SizeOf(Rec2), #0);
Rec1.FontName := 'AName';
Rec1.URL := 'AUrl';
MS := TMemoryStream.Create;
try
// Save Rec1 using an INSERT statement
MS.Write(Rec1, SizeOf(Rec1));
MS.Seek(0, soFromBeginning);
qBlobInsert.Parameters[0].Value := 1;
qBlobInsert.Parameters[1].LoadFromStream(MS, ftBlob);
qBlobInsert.SQL.Text := scInsert;
qBlobInsert.ExecSQL;
// Read saved data back into Rec2
qBlobRead.SQL.Text := Format(scSelect, [1]);
qBlobRead.Open;
MS.Clear;
TBlobField(qBlobRead.FieldByName('blob')).SaveToStream(MS);
MS.Seek(0, soFromBeginning);
MS.Read(Rec2, MS.Size - 1);
Caption := Rec2.FontName + ':' + Rec2.URL;
finally
MS.Free;
end;
end;
Extract from DFM
object qBlobInsert: TADOQuery
Connection = ADOConnection1
Parameters = <
item
Name = 'id'
DataType = ftInteger
Value = Null
end
item
Name = 'blob'
DataType = ftBlob
Value = Null
end>
Left = 56
Top = 32
end
I'm trying to create a record in a database using a SQL query in Delphi 7. I'm using an ADO Query and I've tried both with and without parameters, but to no avail. The error occurs between ShowMessage 1 and 2.
sName := ledName.Text;
sSurname := ledSurname.Text;
sSchool := ledSchool.Text;
sMotherName := ledMotherName.Text;
sMotherCell := ledMotherCell.Text;
sMotherEmail := ledMotherEmail.Text;
sFatherName := ledFatherName.Text;
sFatherCell := ledFatherCell.Text;
sFatherEmail := ledFatherEmail.Text;
sAddress := ledAddress.Text;
sBirthday := DateToStr(dpcBirthday.Date);
ShowMessage(sBirthday);
case rgpGender.ItemIndex of
0 : cGender := 'M';
1 : cGender := 'F';
end;
iGrade := rgpGrade.ItemIndex - 2;
if chkLeader.Checked = true then
bLeader := True
else
bLeader := False;
with dmData do begin
ShowMessage('1');
qryMain.Active := False;
qryMain.SQL.Text := 'SELECT * FROM Users Where Name = "'+sName+'", Surname = "'+sSurname+'", Birthday = "'+sBirthday+'" ';
qryMain.Open;
if qryMain.RecordCount = 0 then begin
qryMain.Close;
ShowMessage('2');
//qryMain.SQL.Text := 'INSERT INTO Users(Name, Surname, MotherName, FatherName, Gender, Grade, Birthday, School, Address, MotherCell, FatherCell, MotherEmail, FatherEmail, NameTag, Volunteer) VALUES("'+sName+'", "'+sSurname+'", "'+sMotherName+'", "'+sFatherName+'", "'+cGender+'", "'+IntToStr(iGrade)+'", "'+sBirthday+'", "'+sSchool+'", "'+sAddress+'", "'+sMotherCell+'", "'+sFatherCell+'", "'+sMotherEmail+'", "'+sFatherEmail+'", False, "'+BoolToStr(bLeader)+'") ';
qryMain.SQL.Text := 'INSERT INTO Users(Name, Surname, MotherName, FatherName, Gender, Grade, Birthday, School, Address, MotherCell, FatherCell, MotherEmail, FatherEmail, NameTag, Volunteer) ' + 'VALUES(:Name, :Surname, :MotherName, :FatherName, :Gender, :Grade, :Birthday, :School, :Address, :MotherCell, :FatherCell, :MotherEmail, :FatherEmail, False, :Leader) ';
qryMain.Parameters.ParamByName('Name').Value := sName;
qryMain.Parameters.ParamByName('Surname').Value := sSurname;
qryMain.Parameters.ParamByName('MotherName').Value := sMotherName;
qryMain.Parameters.ParamByName('FatherName').Value := sFatherName;
qryMain.Parameters.ParamByName('Gender').Value := cGender;
qryMain.Parameters.ParamByName('Grade').Value := iGrade;
qryMain.Parameters.ParamByName('Birthday').Value := sBirthday;
qryMain.Parameters.ParamByName('School').Value := sSchool;
qryMain.Parameters.ParamByName('Address').Value := sAddress;
qryMain.Parameters.ParamByName('MotherCell').Value := sMotherCell;
qryMain.Parameters.ParamByName('FatherCell').Value := sFatherCell;
qryMain.Parameters.ParamByName('MotherEmail').Value := sMotherEmail;
qryMain.Parameters.ParamByName('FatherEmail').Value := sFatherEmail;
qryMain.Parameters.ParamByName('Leader').Value := bLeader;
ShowMessage('3');
qryMain.ExecSQL;
qryMain.SQL.Text := 'SELECT * FROM Users';
qryMain.Open;
The commented out part was the one way I tried doing this, and it gave this error:
Syntax error (comma) in query expression 'Name="Derp",Surname="Foo",Birthday="1900-01-01"'
The code with parameters gives me this error:
Syntax error (comma) in query expression 'Name="Derp",Surname="Foo",Birthday="1900-01-01"'
Any help would be greatly appreciated!
Now that we can see your real code, your error is clear
'SELECT * FROM Users Where Name = "'+sName+
'", Surname = "'+sSurname+
'", Birthday = "'+sBirthday+'" ';
should use the AND operator to link those conditions :
'SELECT * FROM Users Where Name = "'+sName+
'" AND Surname = "'+sSurname+
'" AND Birthday = "'+sBirthday+'" ';
You should also, naturally, seriously consider parameterizing this query as well.
Using TADOCommand to update a record.
using TADOCommand to insert a new record.
Name of table is Board
Using MS Access database
I get an error
Syntax error in UPDATE/INSERT INTO statement
I can connect and retrieve data just fine. Just never added or updated data before.
My database columns looks like this
ID (auto number)
SN (text)
CardType (text)
Desc (memo)
dbDate (date)
Tech (text)
Code looks like this:
procedure TForm2.BSaveClick(Sender: TObject);
const
sqlStringNew = 'INSERT INTO Board (SN,CardType,Desc,dbDate,Tech) VALUES (:aSN,:aCardType,:aDesc,:aDate,:aTech);';
sqlStringUpdate = 'UPDATE Board SET SN=:aSN, CardType=:aCardType, Desc=:aDesc, dbDate=:aDate, Tech=:aTech WHERE ID = :aID;';
var
ADOCommand : TAdoCommand;
begin
ADOCommand := TADOCommand.Create(nil);
// updating a board
if NewBoard = false then
begin
try
ADOCommand.Connection := adoConnection1;
ADOCommand.Parameters.Clear;
ADOCommand.Commandtext := sqlStringUpdate;
ADOCommand.ParamCheck := false;
ADOCommand.Parameters.ParamByName('aSN').Value := ESerialNumber.Text;
ADOCommand.Parameters.ParamByName('aCardType').Value := ECardType.Text;
ADOCommand.Parameters.ParamByName('aDesc').Value := MDescription.Text;
ADOCommand.Parameters.ParamByName('aDate').Value := strtodate(EDate.Text);
ADOCommand.Parameters.ParamByName('aTech').Value := ETech.Text;
ADOCommand.Parameters.ParamByName('aID').Value := UpdateID;
ADOCommand.Execute;
finally
ADOCommand.Free;
end;
Showmessage('Update Complete');
end;
//if a new board
if NewBoard = True then
Begin
try
ADOCommand.Connection := adoConnection1;
ADOCommand.Parameters.Clear;
ADOCommand.Commandtext := sqlStringNew;
ADOCommand.ParamCheck := false;
ADOCommand.Parameters.ParamByName('aSN').Value := ESerialNumber.Text;
ADOCommand.Parameters.ParamByName('aCardType').Value := ECardType.Text;
ADOCommand.Parameters.ParamByName('aDesc').Value := MDescription.Text;
ADOCommand.Parameters.ParamByName('aDate').Value := strtodate(EDate.Text);
ADOCommand.Parameters.ParamByName('aTech').Value := ETech.Text;
ADOCommand.Execute;
finally
ADOCommand.Free;
end;
NewBoard := false;
BSave.Enabled := false;
NoEdit;
Showmessage('New Record Added');
End;
end;
It is advisable not to use SQL keywords for table and field names. DATE is a function in many SQL dialects. If you choose to use a reserved word/function name for a table/field name, you have to escape it in SQL statement: [Date] for SQL Server and MS Access, "Date" - for Oracle.
I want to store images in a database using sql but cant seem to get it to work:
qry.SQL.Clear;
qry.Sql.Add('update tbl set pic = :blobVal where id = :idVal');
qry.Parameters.ParamByName('idVal')._?:=1;
.Parameters has no .asinteger like .Param has but .Param isn't compatible with a TADOquery - to workaround I tried:
a_TParameter:=qry.Parameters.CreateParameter('blobval',ftBlob,pdinput,SizeOf(TBlobField),Null);
a_TParam.Assign(a_TParameter);
a_TParam.asblob:=a_Tblob;
qry.ExecSql;
This also doesnt work:
qry.SQL.Clear;
qry.Sql.Add('update tbl set pic = :blobVal where id = 1')
qry.Parameters.ParamByName('blobVal').LoadFromStream(img as a_TFileStream,ftGraphic);//ftblob
//or
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg',ftgrafic);//ftblob
qry.ExecSql;
Should be something like:
qry.Parameters.Clear;
qry.Parameters.AddParameter.Name := 'blobVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
// or load from stream:
// qry.Parameters.ParamByName('blobVal').LoadFromStream(MyStream, ftBlob);
qry.Parameters.AddParameter.Name := 'idVal';
qry.Parameters.ParamByName('idVal').Value := 1;
qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.ExecSQL;
To read the BLOB back from the DB:
qry.SQL.Text := 'select id, pic from tbl where id = 1';
qry.Open;
TBlobField(qry.FieldByName('pic')).SaveToFile('c:\sample_2.jpg');
I'm using Lazarus, not Delphi, but I guess its usually the same syntax. If so, here's a slight improvement on kobiks suggestion:
Parameters are added automatically if the SQL.Text is assigned before trying to assign values to the parameters. Like this:
qry.Parameters.Clear;
qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
qry.Parameters.ParamByName('idVal').Value := 1;
qry.ExecSQL;
I wrote this as an answer to this q,
Delphi save packed record as blob in a sql database
which is currently flagged as a duplicate, possibly incorrectly because the technique
used by the OP as described in comments appears to be correct. So, the cause of the problem may lie elsewhere.
If the Duplicate flag gets removed, I'll re-post this answer there.
The following code works fine for me against a Sql Server table defined as shown below.
The data from Rec1 is saved into the table and correctly read back into Rec2.
(* MS Sql Server DDL
CREATE TABLE [blobs] (
[id] [int] NOT NULL ,
[blob] [image] NULL ,
CONSTRAINT [PK_blobs] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
*)
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
qBlobInsert: TADOQuery;
qBlobRead: TADOQuery;
Button1: TButton;
procedure Button1Click(Sender: TObject);
[...]
type
TMyRecord = packed record
FontName: string[30];
FontSize: word;
FontColor: integer;
FontStyle: word;
Attachement: string[255];
URL: string[255];
end;
const
scInsert = 'insert into blobs(id, blob) values(:id, :blob)';
scSelect = 'select * from blobs where id = %d';
procedure TForm1.Button1Click(Sender: TObject);
begin
TestInsert;
end;
procedure TForm1.TestInsert;
var
Rec1,
Rec2 : TMyRecord;
MS : TMemoryStream;
begin
FillChar(Rec1, SizeOf(Rec1), #0);
FillChar(Rec2, SizeOf(Rec2), #0);
Rec1.FontName := 'AName';
Rec1.URL := 'AUrl';
MS := TMemoryStream.Create;
try
// Save Rec1 using an INSERT statement
MS.Write(Rec1, SizeOf(Rec1));
MS.Seek(0, soFromBeginning);
qBlobInsert.Parameters[0].Value := 1;
qBlobInsert.Parameters[1].LoadFromStream(MS, ftBlob);
qBlobInsert.SQL.Text := scInsert;
qBlobInsert.ExecSQL;
// Read saved data back into Rec2
qBlobRead.SQL.Text := Format(scSelect, [1]);
qBlobRead.Open;
MS.Clear;
TBlobField(qBlobRead.FieldByName('blob')).SaveToStream(MS);
MS.Seek(0, soFromBeginning);
MS.Read(Rec2, MS.Size - 1);
Caption := Rec2.FontName + ':' + Rec2.URL;
finally
MS.Free;
end;
end;
Extract from DFM
object qBlobInsert: TADOQuery
Connection = ADOConnection1
Parameters = <
item
Name = 'id'
DataType = ftInteger
Value = Null
end
item
Name = 'blob'
DataType = ftBlob
Value = Null
end>
Left = 56
Top = 32
end
I am working on a search query function in Delphi 7 (working with a Paradox database) and I keep getting a type mismatch error when selecting between two dates. If I use the date type I get
Project Project1.Exe raised exception class EDBEngineError with message 'Type mismatch in expression.'. Process stoped.'
If i use a dateTime type I get
Project Project1.Exe raised exception class EDBEngineError with message 'Invalid use of keyword. Token : 13? AND Line Number: 8'. Process stoped.'
where 13 is the first digit of the time.
Here's my code:
procedure TForm1.Button1Click(Sender: TObject);
var
Search1 :string;
Search2 :string;
outputveld : string;
datum : TDateTime;
datumZoek: TdateTime;
countmails : integer;
outfile: textfile;
Zoek6MaandenTerug: Double;
begin
Zoek6MaandenTerug := 182.621099;
datum := tdate(now);
datumZoek := datum - Zoek6MaandenTerug;
ShowMessage(DateTimeToStr(Datum));
ShowMessage(DateTimeToStr(datumZoek));
Memo1.Lines.Add(DateTimeToStr(Datum));
//datum := datum- StrToDate('21-4-2004');
{radio button date controll}
{//radio button date controll}
Search1 := Edit1.Text;
Search2 := Edit2.Text;
assignfile(outfile,'text\Emails.txt');
rewrite(outfile);
outputveld := '';
countmails := 0;
{sets up and executesSQL query(Query1)}
Query1.close;
Query1.SQL.Clear;
memo1.Clear;
if Search1 <> EmptyStr then
begin
//Query1.SQL.add('SELECT * FROM Verkoop');
Query1.SQL.add('SELECT DISTINCT Verkoophandelingen.Klantnr, Verkoophandelingen.Type, verkoop.Klantnr, Verkoop.Artikelnr, Artikels.Nummer, Artikels.artikelgroep, Verkoophandelingen.Datum, Klanten.Email');
Query1.SQL.add('FROM Verkoop');
Query1.SQL.add('full Join Artikels ON Verkoop.Artikelnr = Artikels.Nummer');
Query1.SQL.add('full Join Klanten ON Verkoop.Klantnr = Klanten.Nummer');
Query1.SQL.add('full Join Verkoophandelingen ON Verkoop.verkoophandelingnr = Verkoophandelingen.nummer');
Query1.SQL.add('WHERE Verkoophandelingen.Type = "Bestelling" ');
Query1.SQL.add('AND Verkoop.Artikelnr = '+Search1+'');
//Query1.SQL.add('AND Verkoophandelingen.Datum = '+ DateToStr(Date1) +'');
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN '+DateTimeToStr(datum)+'');
Query1.SQL.Add('AND '+DateToStr(datumzoek)+'');
Query1.SQL.add('ORDER BY Datum');
Query1.RequestLive := true;
Query1.open;
end
else if Search2 <> EmptyStr then
begin
Query1.SQL.add('SELECT DISTINCT Verkoophandelingen.Klantnr, Verkoophandelingen.Type, verkoop.Klantnr, Verkoop.Artikelnr, Artikels.Nummer, Artikels.artikelgroep, Verkoophandelingen.Datum, Klanten.Email');
Query1.SQL.add('FROM Verkoop');
Query1.SQL.add('full Join Artikels ON Verkoop.Artikelnr = Artikels.Nummer');
Query1.SQL.add('full Join Klanten ON Verkoop.Klantnr = Klanten.Nummer');
Query1.SQL.add('full Join Verkoophandelingen ON Verkoop.verkoophandelingnr = Verkoophandelingen.nummer');
Query1.SQL.add('WHERE Verkoophandelingen.Type = "Bestelling" ');
Query1.SQL.add('AND Artikels.ArtikelGroep = '+Search2+'');
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN '+DateToStr(datum)+'');
Query1.SQL.Add('AND '+DateToStr(datumZoek)+'');
Query1.SQL.add('ORDER BY Datum');
Query1.RequestLive := true;
Query1.open;
end;
while not Query1.Eof do
begin
if Query1.FieldByName('Email').AsString <> EmptyStr then
begin
memo1.Lines.Add(Query1.FieldByName('Email').AsString + ';');
writeln(outfile, Query1.FieldByName('Email').AsString+ ';');
Query1.next;
inc(countmails);
end
else
begin
Query1.next;
end;
end;
if Query1.Eof then
begin
CloseFile(outfile);
memo1.lines.add('totaal aantal valid email adressen = ' + IntToStr(countmails));
end;
end;
I hope im posting in the right place.
This is my code after adding parameters for my query still getting
'Type mismatch in expression.'.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables, DBCtrls;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
DBGrid1: TDBGrid;
Button1: TButton;
ComboBox1: TComboBox;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure ComboBox1Change(Sender:TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit2Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ComObj;
{$R *.dfm}
procedure TForm1.FormActivate(Sender: TObject);
var
i : integer;
mystringlist : tstringlist;
datum: TDateTime;
Zoek6MaandenTerug : Double;
begin
Zoek6MaandenTerug := 182.621099;
datum := tdate(now);
datum := datum - Zoek6MaandenTerug;
ShowMessage(DateToStr(datum));
Memo1.Lines.Add(DateTimeToStr(Datum));
Memo1.Lines.Add(DateToStr(datum));
//datum := datum- StrToDate('21-4-2004');
MyStringList := TStringList.Create;
{
memo1.Clear;
Edit1.Clear;
Edit2.Clear;
}
try
Session.GetAliasNames(MyStringList);
{ fill a list box with alias names for the user to select from }
for I := 0 to MyStringList.Count - 1 do begin
combobox1.Items.Add(MyStringList[I]);
end
finally
MyStringList.Free;
end;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
try
Query1.SQL.Clear;
Query1.Databasename := string(combobox1.items[combobox1.ItemIndex]);
except
with Application do
begin
NormalizeTopMosts;
MessageBox(' wrong database ', 'fout..', MB_OK);
RestoreTopMosts;
combobox1.SetFocus;
Exit;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Search1 :String;
Search2 :String;
outputveld : string;
datum : TDateTime;
datumZoek: TDateTime;
countmails : integer;
outfile: textfile;
Zoek6MaandenTerug: Double;
begin
Zoek6MaandenTerug := 182.621099;
datum := tdate(now);
datumZoek := datum - Zoek6MaandenTerug;
ShowMessage(DateTimeToStr(Datum));
ShowMessage(DateTimeToStr(datumZoek));
Memo1.Lines.Add(DateToStr(datum));
Memo1.Lines.Add(DateToStr(datumZoek));
//datum := datum- StrToDate('21-4-2004');
{//radio button date controll}
Search1 := Edit1.Text;
Search2 := Edit2.Text;
assignfile(outfile,'text\Emails.txt');
rewrite(outfile);
outputveld := '';
countmails := 0;
{sets up and executesSQL query(Query1)}
Query1.close;
Query1.SQL.Clear;
memo1.Clear;
if Search1 <> EmptyStr then
begin
//Query1.SQL.add('SELECT * FROM Verkoop');
Query1.SQL.add('SELECT DISTINCT Verkoophandelingen.Klantnr, Verkoophandelingen.Type, verkoop.Klantnr, Verkoop.Artikelnr, Artikels.Nummer, Artikels.artikelgroep, Verkoophandelingen.Datum, Klanten.Email');
Query1.SQL.add('FROM Verkoop');
Query1.SQL.add('full Join Artikels ON Verkoop.Artikelnr = Artikels.Nummer');
Query1.SQL.add('full Join Klanten ON Verkoop.Klantnr = Klanten.Nummer');
Query1.SQL.add('full Join Verkoophandelingen ON Verkoop.verkoophandelingnr = Verkoophandelingen.nummer');
Query1.SQL.add('WHERE Verkoophandelingen.Type = "Bestelling" ');
Query1.SQL.add('AND Verkoop.Artikelnr = :Search1');
//Query1.SQL.add('AND Verkoophandelingen.Datum = '+ DateToStr(Date1) +'');
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN :datum AND :datumzoek');
Query1.SQL.add('ORDER BY Datum');
Query1.ParamByName('datumzoek').Value := datumzoek;
Query1.ParamByName('datum').Value := datum;
Query1.ParamByName('Search1').Value := Search1;
Query1.RequestLive := true;
Query1.open;
end
else if Search2 <> EmptyStr then
begin
Query1.SQL.add('SELECT DISTINCT Verkoophandelingen.Klantnr, Verkoophandelingen.Type, verkoop.Klantnr, Verkoop.Artikelnr, Artikels.Nummer, Artikels.artikelgroep, Verkoophandelingen.Datum, Klanten.Email');
Query1.SQL.add('FROM Verkoop');
Query1.SQL.add('full Join Artikels ON Verkoop.Artikelnr = Artikels.Nummer');
Query1.SQL.add('full Join Klanten ON Verkoop.Klantnr = Klanten.Nummer');
Query1.SQL.add('full Join Verkoophandelingen ON Verkoop.verkoophandelingnr = Verkoophandelingen.nummer');
Query1.SQL.add('WHERE Verkoophandelingen.Type = "Bestelling" ');
Query1.SQL.add('AND Artikels.ArtikelGroep = :Search2');
//Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN '+DateToStr(datum)+'');
//Query1.SQL.Add('AND '+DateToStr(datumZoek)+'');
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN :datum AND :datumzoek');
Query1.SQL.add('ORDER BY Datum');
Query1.ParamByName('datumzoek').Value := datumzoek;
Query1.ParamByName('datum').Value := datum;
Query1.ParamByName('Search2').Value := Search2;
Query1.RequestLive := true;
Query1.open;
end;
while not Query1.Eof do
begin
if Query1.FieldByName('Email').AsString <> EmptyStr then
begin
memo1.Lines.Add(Query1.FieldByName('Email').AsString + ';');
writeln(outfile, Query1.FieldByName('Email').AsString+ ';');
Query1.next;
inc(countmails);
end
else
begin
Query1.next;
end;
end;
if Query1.Eof then
begin
CloseFile(outfile);
memo1.lines.add('totaal aantal valid email adressen = ' + IntToStr(countmails));
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit2.Text := '';
end;
procedure TForm1.Edit2Change(Sender: TObject);
begin
Edit1.Text := '';
end;
end.
after adding this
...
Query1.ParamByName('datumzoek').DataType := ftDate;
Query1.ParamByName('datum').DataType := ftDate;
Query1.ParamByName('Search1').DataType := ftInteger;
Query1.ParamByName('datumzoek').Value := datumzoek;
Query1.ParamByName('datum').Value := datum;
Query1.ParamByName('Search1').Value := Search1;
...
the query gets run but with no results, after showing the query,text it seems the parameters have a "?" value ?
...
SELECT DISTINCT Verkoophandelingen.Klantnr, Verkoophandelingen.Type, verkoop.Klantnr, Verkoop.Artikelnr, Artikels.Nummer, Artikels.artikelgroep, Verkoophandelingen.Datum, Klanten.Email
FROM Verkoop
full Join Artikels ON Verkoop.Artikelnr = Artikels.Nummer
full Join Klanten ON Verkoop.Klantnr = Klanten.Nummer
full Join Verkoophandelingen ON Verkoop.verkoophandelingnr = Verkoophandelingen.nummer
WHERE Verkoophandelingen.Type = "Bestelling"
AND Verkoop.Artikelnr = ?
AND Verkoophandelingen.Datum BETWEEN ? AND ?
ORDER BY Datum
...
Perhaps these lines cause the issue:
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN '+DateTimeToStr(datum)+'');
Query1.SQL.Add('AND '+DateToStr(datumzoek)+'');
Here you are inserting dates as returned by DateTimeToStr and DateToStr, but you are not delimiting the inserted values in any way, and so the resulting query will look something like this:
...
AND Verkoophandelingen.Datum BETWEEN 21-04-2004
AND 22-04-2004
...
I'm not sure what delimiter Paradox uses for date constants, but I'm almost sure it does use some. Perhaps, it should be ':
...
AND Verkoophandelingen.Datum BETWEEN '21-04-2004'
AND '22-04-2004'
...
Check with the manual for the correct one and fix your code accordingly.
On the other hand, it would be a much better idea to use parametrised queries, as #Rob Kennedy has correctly suggested. In a parametrised query, you use placeholders like :name where argument values should go. So, in your case it might look like this:
...
Query1.SQL.add('WHERE Verkoophandelingen.Type = "Bestelling" ');
Query1.SQL.add('AND Verkoop.Artikelnr = :Search');
Query1.SQL.add('AND Verkoophandelingen.Datum BETWEEN :date1');
Query1.SQL.Add('AND :date2');
...
Before running the query, you'll need to set up the parameters using the TQuery.Params property, something like this:
Query1.Params.CreateParam(ftInteger, 'Search', ptInput).AsInteger := StrToInt(Search1);
Query1.Params.CreateParam(ftDateTime, 'date1', ptInput).AsDateTime := datum;
Query1.Params.CreateParam(ftDateTime, 'date2', ptInput).AsDateTime := datumzoek;
Or, if the Query component auto-fills the Params collection when assigning the SQL statement:
Query1.Params.ParamByName('Search').AsInteger := StrToInt(Search1);
Query1.Params.ParamByName('date1').AsDateTime := datum;
Query1.Params.ParamByName('date2').AsDateTime := datumzoek;
That way you won't need to worry about delimiting values: the component will take care of that.