Using Databases in Delphi: Procedure Tform1.Button1Click (Sender: Tobject)
Using Databases in Delphi: Procedure Tform1.Button1Click (Sender: Tobject)
Gr. 12
6. DELETE RECORD MEETING SEARCH CRITERIA
procedure TForm1.Button1Click(Sender: TObject); begin ADOTable1.First; while not ADOTable1.Eof do begin if ADOTable1['Name'] = Edit1.Text then begin ADOTable1.Delete; Exit; end else ADOTable1.Next; end; end;
OR (if the contents of the ComboBox should not be transferred as it is to the database)
procedure TForm1.Button1Click(Sender: TObject); begin ADOTable1.Append; Case ComboBox1.ItemIndex of 0 : ADOTable1['Grade'] := '10'; 1 : ADOTable1['Grade'] := '11'; 2 : ADOTable1['Grade'] := '12'; end; ADOTable1.Post; end;
7. DELETE RECORD CURRENTLY IN FOCUS 7.1 Click on record then click on Button1:
procedure TForm1.Button3Click(Sender: TObject); begin ADOTable1.Delete; end;
8. FILTER RESULTS 8.1 Filter according to exact search criteria (e.g. Name = 'John'):
procedure TForm1.Button3Click(Sender: TObject); begin ADOTable1.Filter := 'Name = ' + '''' + Edit1.Text + ''''; ADOTable1.Filtered := True; end;
Wildcard: %
8.2 Filter according to similar search criteria (e.g. Name LIKE 'Jo%'):
2. ADD RECORD TO TABLE USING CODING 2.1 A new record can be added using set data:
procedure TForm1.Button1Click(Sender: TObject); begin ADOTable1.Append; ADOTable1['Name'] := 'John'; ADOTable1['Surname'] := 'Doe'; ADOTable1.Post; end;
procedure TForm1.Button3Click(Sender: TObject); begin ADOTable1.Filter := 'Name LIKE ' + '''' + Edit1.Text + '%'''; ADOTable1.Filtered := True; end;
begin ADOTable1.First; while not ADOTable1.Eof do begin if ADOTable1['Name'] <> '' then ADOTable1.Delete else ADOTable1.Next; end; end; 10. COUNT RECORDS
procedure TForm1.Button3Click(Sender: TObject); var iCount : Integer; begin iCount := 0; ADOTable1.First; while not ADOTable1.Eof do begin if ADOTable1['Name'] <> '' then iCount := iCount + 1; ADOTable1.Next; end; Label1.Caption := IntToStr(iCount); end; J. Olivier (2009)
begin ADOTable1.First; While not ADOTable1.Eof do begin ADOTable1.Edit; ADOTable1['Total'] := ADOTable1['Unit'] * ADOTable1['Amount']; ADOTable1.Next; end; end;
Gr. 12
11. 8 Aggregate functions
Count number of records
SELECT Count(*) FROM tblResults WHERE Num2 > 50;
11.4 Change data (Amount field becomes 10) according to a condition (Unit equals to 4)
procedure TForm1.Button3Click(Sender: TObject); begin with ADOQuery1 do begin Active := false; SQL.Clear; SQL.Add('UPDATE tblTableName'); SQL.Add('SET Amount=10 WHERE Unit=4'); ExecSQL end; ADOTable1.Refresh; end;
11.9 Date
Year returns year from a date field Month returns year from a date field Day returns year from a date field For example:
Select Name, Day(DateBorn) AS DayBorn FROM tblResults;
11.2.3 Show all fields for records meeting certain exact criteria
SELECT * FROM tblTableName WHERE Name = "John";
11.2.5 Show all fields and all records sorted according to a field (ascending)
SELECT * FROM tblTableName ORDER BY Name;
11.2.6 Show all fields and all records sorted according to a field (descending)
SELECT * FROM tblTableName ORDER BY Name DESC;
11.2.7 Show all fields and all records within a set range (Unit is an Integer field)
SELECT * FROM tblTableName WHERE Unit BETWEEN 1 AND 6;
J. Olivier (2009)
Gr. 12
var Form1: TForm1; arrClasses : array[1..10] of TSingClass; {Array of objects} iCount : Integer; {Count number of objects} implementation
{$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var fFile : TextFile; sTemp, sClassI : String; iJudge1I, iJudge2I, iJudge3I : Integer; begin AssignFile(fFile,'oopdata.dat'); {Open text file} Reset(fFile); While not eof(fFile) do begin Readln(fFile,sTemp); inc(iCount); sClassI := copy(sTemp,1,pos(',',sTemp)-1); {Separate data fields; commas out} Delete(sTemp,1,pos(',',sTemp)); iJudge1I := StrToInt (copy(sTemp,1,pos(',',sTemp)-1)); Delete(sTemp,1,pos(',',sTemp)); iJudge2I := StrToInt (copy(sTemp,1,pos(',',sTemp)-1)); Delete(sTemp,1,pos(',',sTemp)); iJudge3I := StrToInt(sTemp); arrClasses[iCount] := TSingClass.Create (sClassI,iJudge1I,iJudge2I,iJudge3I); end; Closefile(fFile); end; procedure TForm1.FormCreate(Sender: TObject); begin iCount := 0; {Number of objects to zero} end;
2. OOP Example
Create a program that uses OOP to simulate a competition between school class sections.
procedure TForm1.Button2Click(Sender: TObject); var iForCount : Integer; begin FOR iForCount := 1 to iCount do {Calculate averages for all objects} begin arrClasses[iForCount].calculateAverage; end; end; procedure TForm1.Button4Click(Sender: TObject); var iForCount : Integer; sTemp : String; begin RichEdit1.Clear; RichEdit1.Paragraph.TabCount := 4; {For TABS used} RichEdit1.Paragraph.Tab[0] := 70; {First tab in points from left margin} RichEdit1.Paragraph.Tab[1] := 110; RichEdit1.Paragraph.Tab[2] := 160; RichEdit1.Paragraph.Tab[3] := 210; RichEdit1.Lines.Add('Class' + #9 + 'J1' + #9 + 'J2' + #9 + 'J3' + #9 + 'Average'); FOR iForCount := 1 to iCount do begin sTemp := arrClasses[iForCount].getOutput; RichEdit1.Lines.Add(sTemp) end; end; procedure TForm1.Button3Click(Sender: TObject); var iForCount,iJudge,iNewMark : Integer; sClassName : String; begin sClassName := Edit1.Text; {Class name} iJudge := RadioGroup1.ItemIndex+1; {Add one because index start at 0} iNewMark := SpinEdit1.Value; {New mark allocated by judge} FOR iForCount := 1 to iCount do begin if arrClasses[iForCount].getClass = sClassName then arrClasses[iForCount].setJudge(iJudge,iNewMark); end; end; end.
- Create a program that uses OOP techniques. Use the datafile (oopdata.dat) to populate an array of objects. The object must be constructed with a parameterized constructor. Add SysUtils to allow calculations and conversions in your program. - Create the object unit that includes the following private fields: fClass : String; fJudge1 : Integer; fJudge2 : Integer; fJudge3 : Integer; fAverage : Integer; - Add the following methods: constructor Create(sClass : String; iJudge1,iJudge2,iJudge3 : Integer);
MAIN UNIT unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, SingU, Spin, ExtCtrls; {Add object unit name} type TForm1 = class(TForm) Button1: TButton; RichEdit1: TRichEdit; Button2: TButton; Button3: TButton; Button4: TButton; RadioGroup1: TRadioGroup; Edit1: TEdit; SpinEdit1: TSpinEdit; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
Calculate the average (fAverage) from fJudge1, fJudge2, fJudge3 remember they are all whole numbers.
- Design a user interface (main unit) that allows to load the data from the textfile (oopdata.dat) into an array of objects (arrClasses). Use the [Load data] button for this purpose. - Use the [Get Average] button to invoke the calculateAverage method for all the objects in the array. - Use the [Show All] button and use the getOutput method to display all the objects in the RichEdit. Set adequate tabs. - Use the [Set] button to change the score of the class specified in the Edit box for the Judge specified in the Radiogroup. Use the getClass method to find the class and invoke setJudge to change the value.
J. Olivier (2009)
J. Olivier (2009)
Gr. 12
Val Converts string value to integer or real. Also checks for errors. Val(sOriginal, iNum, iError); Val(sOriginal, rNum, iError); Round Rounds real number to the nearest integer Round(4.8); Roundto Rounds to a set power of 10 iH := Roundto(2745,3); Inc Increases value of variable (You can also do this as: iNumber := iNumber + 1;) Inc(iNumber) Dec Decreases value of variable (You can also do this as: iNumber := iNumber - 1;) Dec(iNumber); Frac Provides decimal part of real number rK := Frac(3.54); Sqr Gives square of number typed in rK := Sqr(16); Sqrt Gives square root of a number rK := Sqrt(16); Power Raises first number to the power of the second number (134) Power(13,4) Pi Provides the value of pi () rW := rK * Pi; Random Provides a random number within a range of 0 and limit-1. Randomize; iX := Random(100); (iX = any number between 0 and 99) Remember to add the Math unit to the uses section of your program when using the RoundTo and Power functions.
2.11.2 Move up
procedure TfrmStringGrid.btnUpClick(Sender: TObject); begin sgdMoveArea.Cells[iPosCol,iPosRow] := ''; iPosRow := iPosRow - 1; sgdMoveArea.Cells[iPosCol,iPosRow] := 'X'; end;
4. String handling
sSourceText := The man walks
- Determine the position of a piece of a text within a string IntegerVariable := Pos(StrToBeFound, SourceText); For example: iX := Pos (m, sSourceText); {The value of iX is now: 5} - Display a certain character within a string using square brackets at the end of a variable. StringVariable := SourceText[CharacterPosition]; For example: sNewText := sSourceText[2]; {The value of sNewText is now: h} - Display a certain section of text within a string. For example: StringVariable := Copy(SourceText, BeginPosition, Length); sNewText := Copy(sSourceText, 5, 3); {The value of sNewText is now: man} - Insert a certain section of text within a string. For example: Insert(InsertText, SourceText, Position); Insert(big , sSourceText, 5); {The value of sSourceText is now: The big man walks} - Remove a certain section of text within a string. For example: Delete(SourceText, Position, Length); Delete(sSourceText, 5, 4); {The value of sSourceText is now: The walks} - Determine the length of a string. For example: IntegerVariable := Length(SourceText); iX := Length(sSourceText); {The value of iX is now: 13} - Change the whole string to lowercase. For example: LowerCase(SourceText); LowerCase(sSourceText); {The value of sSourceText is now: the man walks} - Change the whole string to uppercase. For example: UpperCase(SourceText); UpperCase(sSourceText); {The value of sSourceText is now: THE MAN WALKS} Take note: Use UpCase for Char type.
3. Mathematical functions
StrToInt Converts string value to integer iN := StrToInt(sNumber); IntToStr Converts integer value to string sNumber := IntToStr(iN); StrToFloat Converts string value to real rN := StrToFloat(sNumber); FloatToStr Converts real value to string sNumber := FloatToStr(rN); FloatToStrF Converts real value to string with formatting (1 number after the decimal for example) sN := FloatToStrF(rN, ffFixed, 15, 1); Trunc Truncates (cuts) the decimal point Trunc(4.5);
J. Olivier (2009)