0% found this document useful (0 votes)
3 views17 pages

It Grade 12 Notes

The document provides an overview of classes and SQL in Delphi programming, detailing how to create classes, their attributes and methods, and how to interact with databases using SQL. It explains the structure of SQL queries, including selection, conditions, ordering, and the use of wildcards, as well as various SQL functions and aggregate functions. Additionally, it covers best practices for counting records and formatting data within SQL statements.

Uploaded by

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

It Grade 12 Notes

The document provides an overview of classes and SQL in Delphi programming, detailing how to create classes, their attributes and methods, and how to interact with databases using SQL. It explains the structure of SQL queries, including selection, conditions, ordering, and the use of wildcards, as well as various SQL functions and aggregate functions. Additionally, it covers best practices for counting records and formatting data within SQL statements.

Uploaded by

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

CLASSES:

● Classes can be thought of as the ‘blueprint’ for objects in delphi


○ Every single object you see in Delphi has a class, for example TPanels,
TButtons, ect.
○ Classes outline the things an object is able to do/how a user is able to interact
with the object

● When creating a class, the first thing you do with your application is create a new unit
file
○ You then want to rename the file to something that makes sense/suits the
program you care creating

● Once you have opened a unit file, you want to create a new object. This object will
take the characteristics of the parent object, so you assign it to class(tObject)

● Classes have ATTRIBUTES and METHODS


○ Attributes are the things your objects are made of. For example think of a car, its
attributes would be wheels, doors, engine ect. Attributes have the naming
convention prefix of [ f ]
○ Attributes are declared under the private section on your unit file. This basically
means that your main form is NOT able to access/change the attributes of the
object, and they can only be changed directly through the unit file or through
methods

○ Methods are things that your object is able to do. For example think of a car, its
methods would be driving forwards and backwards, shining lights, playing music
ect. Methods don’t really have naming conventions as they are
functions/procedures
○ Methods are declared under the public section on your unit file. This basically
means that your main form IS able to access/work with the methods your object
is able to carry out.


● There are 3 types of methods:
○ GETTER METHODS / ACCESSOR METHODS - Always functions that are used
to receive the attributes of the object as a variable/results
○ SETTER METHOD / MUTATOR METHODS - Always Procedures that are used
by your main form to indirectly change the attributes of your main form
○ AUXILIARY METHODS - Methods which can be either procedures or functions
and have the main function of making calculations

● To add an object to your main form add the class name under the uses heading in
the code of the main form

● To create an object using your class, assign a variable to your class name

● To use the methods your object consists of, you would write objectname.method
similar to how for a TEdit you could use the .text method, or for a TSpinEdit you could
use the .value method

Databases:

● There are 4 main objects we use when working with databases:


○ ADOConnection: Connects the database to the form
○ DataSource: A component that allows you to display information on a DB grid
○ ADOQuery: Allows you to use SQL to run queries on your code
○ ADOTable: Allows you to run queries on the database using delphi

SQL:
● SQL stands for structured query language and is one of the most common ways
developers query a database

● To use SQL:
● First select which field(s) you are going to be looking at and displaying in the
database. Fields are separated by a comma
○ SELECT First_Name, Last_Name

● Second specify which table you are querying from


○ FROM tblCustomers

● Thirdly, determine which condition you are looking for. This can for example be
checking whether the value in a field is equal to a condition or greater than/less
than a set value. You separate conditions with an AND, and words are placed
between quotation marks and dates are placed between hashtags while
integer values are just written out as is
○ WHERE Gender = “Female” AND DOB > #10/09/2004#

● Finally you determine the way you would like to order your fields. You write this in
full capitals and go ORDER BY (FIELD)
○ ORDER BY DOB

● In SQL, there are certain characters known as wild cards. Wild Cards can represent
any character. There are 2 main types of wild cards:
○ ( _ ) an underscore is a wild card which represents a single character. For
example ‘__Jassem’ would mean that any word containing 2 characters before
Jassem is valid
○ ( % ) a percentage sign is a wild card which represents an undefined number of
characters. For example ‘%@gmail.com’ would mean that any word which ends
with @gmail.com, regardless of how many or what type of characters are before
it is valid
○ When using wildcards you use the word like instead of the = symbol

● When working with a set, so a group of values, you don’t use the = sign, but rather the
IN function
○ The basic format of the function is: WHERE Name IN
(“Jassem”,”Jean”,”Alex”)

● When looping through databases using delphi code:


○ You want to create a nested loop where you loop first through the parent table,
and then through the child table
○ When looping through the child table you need to make sure that the foreign key
in the child table is the same as the primary key in the parent table
○ You usually have a variable tracking values in the tables that you’re looping
through
○ Remember to go to the next record of the table

● If you would like to order the records by a certain field you use the Order By function
○ The format is: Order By Field ASC/DESC
○ The asc stands for ascending and orders the records in ascending order based
on the field and desc stand for descending and does the opposite
○ If you would like to order records by more than one field you use Order By Field
ASC, Field DESC for example which orders the records first by the original field
and then by the secondary field
■ For example if your first field was a company name and the second was
the product description, it would first order all the records by the company
name with the closest letter to a, and then would order the now ordered
fields once more by the product names with the closest letter to z
■ This doesn’t mean it re-orders everything, and the records will still be
ordered by company name, it will just then also be ordered by the product
names after

● If you would like to select only records that are unique from one another when looking at
the selected fields use the SELECT DISTINCT function in your sql statement
○ This replaces the select function that is normally used in sql
○ The line: SELECT DISTINCT PersonName, Surname, School would only
display records where either the persons name, surname or school is different
■ If there were 2 John Doe’s for example, but they go to different schools
the record would still be displayed but if they went to the same school, the
record would only be displayed once
● If you’re working with more than 1 table simultaneously, so a parent and child table, you
will have 2 tables in your ‘FROM’ statement
○ You then need to specify from which table are you taking the shared field, so are
you taking it from the parent key where its a primary key or from the child table
where its a foreign key
○ You also need to check that the primary key and foreign key in both tables
is the same to ensure that you’re getting information that relates to one
another

○ Notice how I reference which table I’m working with (circled in blue) by writing the
table name, dot and then the field name. You can shorten this by typing the
table name, a space and then a value you’d like the table to be referenced
as in your FROM statement

● If you would like to count how many records there are that match your SQL
statement:
○ You can firstly loop through your Query component similarly to how you would a
table. So QueryNAME.first; While Not QryNAME.EOF do and then you have a
variable you increment with each loop and go to the next record


■ Here the variable iCount would be the value of the number of records
which match your SQL statement, not all the records in the table
■ You need to use this method in exams

○ The second method is to use the .RecordCount procedure which will


automatically count how many records match the SQL statement

■ If you equated an integer variable, like iCount to this statement it would
have the value of the number of records which match your SQL
statement, not all the records in the table

● If you would like to perform a calculation with 2 fields in an SQL statement directly
○ First you need to state which 2 fields you are working with and then minus, add,
divide or multiply them
○ Second you need to give this new field you’ve created a name. Put the name in
square brackets with the word AS before it


○ Note you’re not able to refer to this field in your sql statement, you’d have to use
the original 2 fields with the arithmetic symbol present

● When giving a field you have worked with whether that may be through using functions
or arithmetics a new name using the AS function:
○ You can't work with the new field in your SQL statement
■ For example when trying to use the group by function, or when using a
conditional statement like where ect.
○ You can however work with the field in your query component so to display
it on outputs
■ For example RedOut.Lines.Add(qryExcursion[‘NewFieldName’])

🔍EXTRA NOTE

Whenever you are using an aggregate function like count, or sum ect:
- You ALWAYS need to group the aggregate function, or the fields used in the
aggregate function, by another field that isn’t being used in the aggregate function
- This ensures that the answer of the aggregate function isn’t output multiple times as
the full amount, else you’d get an error with your code
- EXAMPLE: SELECT Register class, SUM(AmountPaid) AS [Total Amount] FROM
Learners GROUP BY Register Class
- If you’re using more than 1 field in the select statement, you have to group by one of
the fields in your select statement, but if you’re only working with an aggregate
function field you can group by anything
🔍EXTRA NOTE

If you have 2 fields in a table that you would like to turn into 1 field:
- You can add the 2 field names together and then rename the field
- For example: SELECT RegisterClass + “.” ActivityName AS [Class Activity]
- You will then have a field called Class Activity in your SQL statement that is a
combination of the field Register Class, a fullstop and the field ActivityName

SQL FUNCTIONS:
● FORMAT FUNCTION: (SELECT ….. FORMAT(.......) AS [Name of new field])
○ To Format Currency: FORMAT(TotalPaid *+-/ 0.25, “Currency”)
○ To Format Dates: FORMAT(DateOfBirth, “dd-mmm-yyyy”)
■ 3 d’s will give you the day as a 3 letter word (Tue, Mon, Wed)
■ 3m’s will give you the month as a 3 letter word (Jan, Feb, Mar)
■ 1 d will give you the date as 1 or 2 numbers(1, 12, 24)
■ 1 m will give you the month as 1 or 2 numbers( May = 5, Dec = 12)
■ 2d’s will give you the date as 2 numbers always (01, 05, 12)
■ 2m’s will give the month as 2 numbers always (May = 05, Dec = 12)
■ 4 y’s gives you the full year (2015, 2024)
■ 2y’s gives you the last 2 digits of the year (15, 24)
■ Having dashes or forward slashes determines how number are split and
having none will just leave spaces(12/05/2026 OR 12-05-2026 OR 12 05
2026)
○ To Format A Number To 2 Decimals: FORMAT(LongNumber,”Fixed”)
○ To Format A Time: FORMAT(TimeOpened, “hh:mm AM/PM”)

● ROUND FUNCTION: (SELECT …. ROUND(LongNumber +-*/ 0.25, 3 As [Name of new


field])
○ The round function always rounds up and down normally, unless the decimal is
0,5 or 0,05 or 0,005 ect in which case it rounds up to the closest even number
○ For example 2,5 would round to 2 but 3,5 would round to 4
○ This number determines the number of decimal places that your number is
rounded off to. For example 3 means there should be 3 decimals left over, 2
means 2 should be left, and 0 means there should be no decimals
○ Just like with any other function give your new field a name in [SQUARE
BRACKETS]
● INT FUNCTION: (SELECT …. INT(LongNumber +-*/ 0.25) AS [Name of New Field])
○ The int function basically is like MOD, throws away all of the decimals without
rounding, so it doesn't matter how high or low the decimals are

● STR FUNCTION: (SELECT …. STR(PicFileSize + “KB”) AS [Name of New Field])


○ The STR function basically returns an integer as a string type data type
○ You can add text to the string, like here we add the words KB directly after the
file size

● UCASE FUNCTION: (SELECT …. UCASE(Surname + “...”) AS [Name of New Field])


○ Basically like the uppercase function, changes every letter to uppercase

● LCASE FUNCTION: (SELECT …. LCASE(Surname + “....”) AS [Name of New Field])


○ Basically like the lowercase function, changes every letter to lowercase

● LEFT/RIGHT FUNCTION: (SELECT …. LEFT/RIGHT(String, 1) AS [Name of New


Field])
○ Like the copy function and starts counting from either the left or right side of the
string
○ The number determines how many characters you’re copying
○ Always starts at the first or last character depending on if you use left or right

● LEN FUNCTION: (SELECT …. LEN(CDName) AS [Name of New Field])


○ Function that returns the length of a string

● MID FUNCTION: (SELECT …. MID(String, 3, 6) AS [Name of New Field])


○ Exactly the same as the copy function
○ This number is from which character you’re starting to copy
○ This number is how many characters you’re copying from the starting character
forward, including the starting character

● YEAR, MONTH, DAY FUNCTION: (SELECT …. YEAR/MONTH/DAY(Date) AS [Name


of New Field])
○ Returns the year, the month or the day of a date data type
○ For example YEAR(12-05-2026) would return: 2026
○ For example DAY(12-05-2026) would return: 12 (1 → 31)
○ For example MONTH(12-05-2026) would return: 5 (1 → 12)

● DATE FUNCTION: (SELECT …. DATE() AS [Name of New Field])


○ Returns the current date of the system in a formatted manner
● DATEVALUE FUNCTION: (SELECT …. DATEVALUE(“5/1/1990”) AS [Name of New
Field])
○ Converts a string into a date value data type
○ For example “5/1/1990” would be 5 January 1990
○ Its based on the format of a system
○ Often used when comparing 2 fields, like if the string is equal to a certain date
type field’s entry
SQL AGGREGATE FUNCTIONS:

● COUNT FUNCTION: (SELECT …. COUNT(*) AS [Name of New Field])


○ You can count all the fields in a table
○ You can count all the fields in a table that meet a certain criteria
○ You can count all the fields in a table that don’t have null values by placing a field
name in the count
○ You can count all the fields that do only have a null value by placing the words
NULL in the count

● MAX/MIN FUNCTION: (SELECT …. MAX/MIN(CDCost) AS [Name of New Field])


○ Returns the highest or lowest value found in a certain field
○ Has to be an integer

● SUM FUNCTION: (SELECT …. Sum(CDCost) AS [Name of New Field])


○ Returns the sum of all the values for a specific field in a table

● AVERAGE FUNCTION: (SELECT …. AVG(CDCost) AS [Name of New Field])


○ Returns the average value of all of the records in a certain field

🔍EXTRA NOTE

If you use an aggregate function:


- If you have only 1 field in your select statement, which is your aggregate function you
don’t need to use a group by clause
- If you have more than 1 field in your select statement you NEED to use a group by
clause and group by all of the other fields in your select statement
- For Example: SELECT(Store, ID, Count(Store))
- You would go GROUP BY(Store, ID)

🔍EXTRA NOTE

The layout for an SQL statement is:


- SELECT
- FUNCTION (can be used in where condition)
- AGGREGATE FUNCTION (can be used at having condition)
- FROM
- CONDITION (WHERE)
- GROUP BY
- CONDITION(HAVING)
- ORDER BY

● THE HAVING CLAUSE is used to add a condition to an aggregate function that is


grouped
○ For example: GroupBy(Genre) HAVING SUM(CDCost) > 2000)
○ This will group the sum of the cost of the CD’s by genre, but only display groups
where the total cost of all the CD’s of a certain genre is more than R2000

SQL DATABASE MAINTENANCE:

● INSERTING INTO A DATABASE WITH SQL


○ First: In your Sql.Add statement you start it off with INSERT INTO TABLENAME
■ qryName.SQL.Add('INSERT INTO tablename’)

○ Second: You add a bracket after the table name, and write all of the different
names for the fields in your database
■ ‘(FieldName1, FieldName2, FieldName3, FieldName 4)’

○ Third: You write VALUES(...) with all the values of the new fields. Its important to
note the order you add these values in is crucial and must line up with your field
order originally
■ ‘VALUES(Value1, Value2, Value3, Value4)’
■ NOTE: The values must be in between the right symbols, so text between
quotation marks, dates between hashtags ect

○ Fourth: Write qryName.ExecSQL to save your changes


(LEARN DELETE AND EDIT)


DELPHI CODE WITH DATABASES:

● Let's say you are selecting fields from a certain record, but you only want to take records
that are corresponding, so primary key in the parent table links with the foreign key in the
child table

● If you have a data module(dm) connected to a Database grid (dbGrid) and would like to
work with the selected record on the dbGrid, you can use the
dm[NAME].tblName[‘FieldName’]
○ This will automatically extract the field from the selected record on the dbGrid


○ This code will essentially give the variable sActivity the value of ‘Archery’ as that
is the value of the field [‘ActivityName’] for the selected record in tblActivity
Arrays:
● When trying to remove a value from the array
○ First: Find out the total number of entries in the array and store it
○ Second: Assign a value to the position of the entry in the array that you would
like to delete
○ Third: Loop from the position of the entry you would like to delete, till the total
number of entries in the array -1
○ Fourth: Make each entry in the array, the next entry (ArrName[iLoop] =
arrName[iLoop + 1]
○ Fifth: Make the highest entry now equal to nothing, and decrease the total
number of entries


ARRAYS:
● You can create 2D arrays in delphi rather simply
○ A 2D array is an array which has 2 different parameters as opposed to just one
○ A 2D array will create a grid of parameters for values

1 2 3 4 5 6 7

C John

E
○ For example, looking at this 2D array, if we wanted to insert or work with a
certain value, you would reference it by referencing the block, like a coordinate
○ C6 for example would return the value: John

● To create a 2D array you use the same code when creating a normal array but with 2
Parameters rather than one

● arrNAME : array[‘A’..’E’ , 1..7] of real;


■ This is your arrays name
■ This is the first parameter
■ This is the second parameter
■ This is the data type that is allowed to be stored in your array
● To populate your 2D array you want to use a nested loop
○ You first loop through you first parameter and then have a nested loop inside of it
looping through your second parameter
○ To then actually add values, you use arrName[First Parameter Position,
Second parameter position]

○ The array used in this example is called ar2Tip. The variables used for the loops
are cRow, and iCol. These 2 variables will then be the parameters
■ For example when cRow := C and iCol:= 5, then the block C5 would get
populated by the value from the input box
■ The input box is being used to determine what the value for a certain
block should be

● To actually work with your 2D array, for example when trying to calculate the sum of a
single row:
○ You need to once again loop through the different parameters, looping through
the first and then the second in a nested loop
○ You then put your code in your nested loop and repeat

You might also like