0% found this document useful (0 votes)
9 views8 pages

Delphi Notes

it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Delphi Notes

it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Delphi Notes

By Siyethaba Shozi

Structured Query Language

The first part of SQL, which is question 2.1, deals with SQL (short statements which answer a
desired question). Some methods are regularly used for asking such questions of the database
system, and the format is most often the same.

 SELECT
SELECT <Fields> FROM <table> WHERE <Condition>
Note: The format will always remain the same. Never put two or more SELECT statements in
one statement; it only works for specific conditions that are not usually asked.
The most commonly used statement in this question requires a basic understanding of an IT
student, and it rewards a mark regardless of whether the code runs or not; all you have to do is
be meticulous with the format in which you place your statement. We will look at a few
scenarios:
Scenario 1: Display ALL fields of the students in the database
SELECT * FROM tblStudents
Note: There are a few things to look into here. At first, the statement states it needs an output
with ALL the fields of the database for the student; thus, whenever you see ‘all’ in a statement,
it is important to place an apostrophe in front of the SELECT statement. We use a
SELECT statement because of ‘Display’. After typing SELECT and followed by an
apostrophe, you should reference where you are getting this information from, basically, which
table are you talking about, which in this case must be tblStudents. And before that, the
FROM statement must be used.
Scenario 2: Display ALL fields of students with age above 15
SELECT * FROM tblStudents WHERE Age>15
Note: Just like last time, the first keywords are Display and All, which direct us in using a
SELECT statement and an APOSTROPHE to display all of the fields. Then it follows
with the reference at which you must get the information, which you must use FROM which
table. But in this statement, at the end, there is a condition. The condition will ALWAYS be
treated after the FROM statement, where you will use the field you are referring to and the
condition it must meet.
Scenario 3: Display the name, surname and grade of all learners with an average above 75.
SELECT Name, Surname, Grade FROM tblStudents WHERE Average>75
Note: Once again, there are slight but simple changes. In this statement, once again, the first
thing to note is that they have given you the fields that they want to be displayed, unlike before,
where all the fields were needed. So what you must do is look at the criteria they have given you
and go back to the table and ensure that the field name is the same as the criteria e.g. they said
they want name of student but in the database table it is called StudentName so you are then
forced to used the one in the database table as usual. The fields are separated by commas. Then
the rest of the statement is the same as the previous scenario. Note that the ‘all’ in this statement
refers to learners, not fields.
Scenario 4: Display Company, Branch and Gross profit as a calculated field called Gross Profit
with gross profit above 150,000. Format to currency

SELECT CompanyName, Branch, FORMAT ((Income – Expenses),” Currency”) AS [Gross


Profit] FROM tblCompany HAVING (Income - Expenses)>150000
Note: Now there is a lot to cover here, so let's start with the pseudo field creation. When a
question asks you to display a field that is not present within the database table, it calls for you
to create a so-called pseudo field. A pseudo field arises from a calculation performed from
existing fields, such as Income 7 Expenses, in the database table. For a pseudo field ‘Gross
Profit’ to be formed, it is needed that I take Income and minus it from Expenses, and the same
way it is written is important that you write AS, then you write that pseudo field in brackets.
Then to format, you must write FORMAT in front of your calculation, and the calculation
must be written in parentheses, then you put currency in inverted commas, but if it had decimal
places, it would be 0.00. Then, if the condition uses the pseudo field, you must use the phrase
HAVING after FROM or WHERE if it is there.

Scenario 5: Display the Name and Marks of students from Highest to lowest, where students'
first name starts with A.

SELECT Name, Marks From tblStudents, tblMarks WHERE tblStudents.StudentID =


tblmarks.StudentID AND Name LIKE … Order By Marks Desc
Note: There are a few tweaks, nothing major, just want to introduce a few concepts. The first
thing to address is that after the FROM clause, I have put two tables, indicating that the
information I want to display is on two separate tables, so you must write the first table,
separate it with a comma and then write the second one. After the FROM statement, follow
with a WHERE to state my condition, because I am referring to two tables, there has to be
something that connects the two, and it will always be the primary and foreign key. In this case,
I am referring to StudentID. So whenever we are talking about two tables, you must write a
similar code using the relevant foreign key for those tables after the WHERE statement. Then
the part of Order By addresses the statement from highest to lowest, which means descending
order in mathematical terms. Luckily, in Delphi, there are mathematical functions; in this case,
we are referring to DESC, then we have ASC for ascending order. SO you must always write
it in this format, firstly you must type Order By followed by the field you want to sort, which
is Marks in this case, then at the end how you want to order it, which is DESC or ASC.
Scenario 6: Display the Name, Surname and average marks as AVG Marks of all students. Show
the top 10 marks. There are about 110 students.
SELECT Name, Surname, Top 10 (AVG(Marks)) AS [AVG Marks] FROM
tblStudents
Note: So the first thing is calculating the average of marks there is a function called AVG used to
calculate the average of that field, so you write AVG open parenthesis and put the field you are
calculating because you are calculating you must create a pseudo field of course like the ones before use AS
and put the name of that field. Then the Top function takes the best of what you want; in this case, it's
10, so you must write Top and the number that you need.

Then I can’t cover all of it here, but there is COUNT, DISTINCT, MAX, MIN, SUM, NULL,
NOT, and there is more, but you should look at these; but they are not different in format from the
previous examples.

 Update
UPDATE<tablename>SET<Nameoffield>=<New value> WHERE
<Nameoffield>=<Old value>
The update statement will ALWAYS be the same, like literally the same, there is no SMANGA in
this thing. So what does it mean it says I will update a certain table and in that table I will set one
of the field where the cursor is to a new value but because im updating it is the same as me
replacing therefore there is a condition that I’m updating that field where there is an existing
value.

Scenario: Update all the records with Shozi as their surnames to Smith
Update tblStudents SET Surnames=’Smith’ WHERE Surnames = ‘Shozi’
Note: there isn’t much to dwell on here because it simply says in the table that I will
set the surname to be Smith, where it was previously Shozi as the surname.

 Insert
INSERT INTO <tablename> Values (Write your values in that specific order)
INSERT INTO <tablename> (FIELDS IN ORDER) VALUES (Write your values in
the same order as the fields)
The insert statement is used to add new information to an existing database table. There are two
ways to approach it, depending on the question or scenario. The first Statement is used if the
question has given you all the values for every field in the database table, so there is no need to
specify the fields; you are entering all you have to do is write the values in order and their
respective datatypes. The second statement is commonly used if they are asked to add new
values, and it's not all the fields in the database, then you need to specify the fields and also write
their corresponding values.
Scenario: In a database table with fields (PlayerID, Name, Surname, JerseyNo., DateofBirth,
NumberofGoals, NumberofAssists ), add a new record with the following
playerID=Live456p
Name = Jeremy
Surname = Frimpong
JerseyNo. = 30

INSERT INTO tblPlayers (PlayerID, Name, Surname, JerseyNo.) VALUES (“Live456p”, “


Jeremy”, “Frimpong”, 30);

Note: There are a couple of things to note. The first thing is to check if the values they have given you do they fit
all the fields in the database; if not, then you know you have to use the second statement. In this case, we were
given all the fields in this database, but when the instruction came out for only a few fields not all of them to that
forces you to specify before VALUES in parentheses the columns/fields you are referring to, then after
VALUES in parentheses the values that are in correspondence with the column/fields. What’s important is that
the datatypes for string need double quotation then the integer you write it as is, as well as float.

 Delete
DELETE FROM <tableName> WHERE <Condition>

Simply, the delete statement is very simple, it's deleting a RECORD or RECORDS that meet a
certain criterion. You must specify from which table and what you are looking for (condition)

Scenario: Remove all records with a name starting with A

DELETE FROM tblStudents WHERE Name LIKE “A%”


Note: Delete all records of students that start with A in their names, so the
WHERE statement is there to state the condition.

Object Oriented Programming

Unit Form
Methods (Declarations under Public)
1. Mutator (Settor)
 Mutators are always Procedures
 Keyword: ‘Receive a parameter’
 Declaration: Procedure setPrice (rPrice: real);
 Ctrl + Shift + C: fPrice := rPrice
Note: In the question paper for 3.1, they will never ask you to write specifically a
procedure or method unless you have one, but they will say Write a method that will
receive a parameter for the price of shoes. This statement and the way they ask it gives
you a hint of whether you use a method of procedure or a function. We are focusing on
Mutators right now. The first bullet point illustrates that Mutators will always be
Procedures, but there has to be something in a statement that indicates that. Within
this statement as a whole, there is a part where they say ‘Receive a parameter’. The
word ‘receive’ guides you that it’s a procedure, and the word parameter, otherwise
known as a variable, should be declared so it can hold the value that will be received by
the attribute. A procedure will ALWAYS have a parameter, the same way it is
written up there, and you must declare the datatype of your parameter as illustrated.
Then, after control shift C, all you have to do is assign the respective attribute which is
given to the respective parameter so it can receive that value.
2. Accessor (Gettor)
 Accessors are always functions
 Keyword: ‘Return a Value’
 Declaration: function getPrice: real;
 Ctrl + Shift + C: result:= fPrice;
Note: Write a method that will return a value for the price of shoes. This statement
and the way they ask it gives you a hint of whether you use a method of procedure or a
function. The first bullet illustrates that accessors are functions; if they are functions
once again, there has to be an indicator, in this case, within a statement, they return a
value. The word ‘return’ immediately tells you the method you are about to type is a
function. It's important to see that there is no parameter to be declared or created;
therefore, when typing your method, it will be exactly the way it is shown above at the
end, with the datatype corresponding to the type of result you want. In this case, it is
real. After Control Shift C, you will always equate the result to the attribute that is in
correspondence.
3. Auxiliary
 Auxiliary methods are always function
 Keyword: ‘Return a Value’
o Declaration: Function determineLevel:String;
Function calculatePrice: Real;
Function ToString: String;
 Ctrl + Shift + C:
Note: It is pretty difficult to come up with examples, but things to take note of are that
the auxiliary methods are mostly calculations in the case of determine and calculate.
They will most likely give you a formula, or it will be a common formula, such as a
percentage. The formula or calculation, or whatever you are determining is assigned by
result. E.g. result:=(fMarks/fTotalMarks)*100. To determine, there could also be
an if statement to return a value kuresult. The last auxiliary being tostring is for
structural purposes; it's also always a string datatype. And it is always declared in the
same way. Something to note is that if the attribute is not in string format, it must be
converted into that format of string, the normal way. If there is a mathematical function
that is needed under uses don’t forget to add math.
4. Special Case:
Special case where within a statement/question, there is both ‘receive a parameter’ and ‘return a value’. The
method that is in use will always be a function, but it has a difference from the normal function structure. It’s like
a procedure and a function had a baby. E.g. Function calcPercentage (iMarks: integer): Real;. Things to note are
that there is a parameter(variable) within a function, and the parameter is assigned to its datatype, and outside
that parameter, you must assign a datatype specifically for the function method

5. Constructor Method
Constructor Create (Add Parameters, separate with; between datatypes);
Scenario: Create a constructor with parameters for fName, fSurname, fAge, fMarks, and a
default value for fPercentage should be assigned to 0 and fTotalMarks to 100
Constructor Create (sName, sSurname: string; iAge, iMarks: integer)
Note: First thing, there is generally a problem that there is a space between
the constructor and create, then immediately parentheses are opened, then as
they give you the attributes, which you must create parameters. You must first
know the datatypes of those attributes, so when we create those parameters,
they should have the same datatype. The parameters of the same data type are
separated by commas, and you declare that data type, then you separate the
data type with a semicolon. Be aware that attributes that need DEFAULT
values are not within those parameters, it's only the other ones that are
needed. Then you must declare each attribute with its parameter. E.g.
fName:= sName; then for default value, depending on what the default value
is, you must type E.g. fTotalMarks:= 100; If it’s a string, don’t forget
quotations.
Main Form
1. Declaration of object/class, but it's usually done for you
Var objMarks: TMarks

 Instantiate Object
Instantiating is assigning the class/object variable to the class form using a constructor, which holds the desired
parameters to continue all operations and functions.
So, before instantiating, there is a part of declaring variables or assigning variables to their corresponding
components (edit box, spin edit, combobox, etc ) of the Main Form.
E.g. sName:= edtName.text;
sSurname:=edtSurname.text;
iMarks:= strtoint(edtMarks.text);
iAge:= strtoint(sedAge.value);
After assigning these variables preferably they must be the same variables as the one in the parameter in the
constructor in the unit form. Then you must assign the variable of the object and the constructor.
objMarks:= TMarks.create(sName,sSurname,iAge,iMarks);
Note: Something important is that you will take the variable of the object assign it to the class name
which is found at the top in the declaration or the unit form and press a dot and create then open
parenthesis and carefully enter each variable in ORDER as the ones in the constructor create in the unit
form. Then you can do the same thing without variables by just entering the name of the component in
order and converted into respective datatypes with the parameters.

Then when displaying after instantiating all you have to do is call the ToString method.
redisplay.lines.add(objMarks.ToString);

Then if the question requires for you to call a method all you do is use the variable(objMarks) and use dot the
name of the method
objMarks.<Name of Method>
e.g. objMarks.DetermineLevel;

Then there is a case where there is a method that is being required to be called but it has a setter and a
gettor. If it has that condition you must do the following

objMarks.setPrice(rPrice);
redisplay.lines.add(‘The Price is ’+ #9 + floattostr(objMarks.getPrice));

Note: As always if the method meets the criteria above you must first call the
settor which will receive the parameter value and then the getter will be put in
display to return the value that has been receive

You might also like