Thank you for your great answer xenablaise.
About the code in the tutorials: In the first tutorial, the code was just the automatically generated code from Lazarus. In the third the only extra code is a few lines to launch the report in runtime.
In the second tutorial a lot of the code is used to have the queries run for 10 seconds and display the result and is not so relevant for a database tutorial. The only database code is:
1. The statements to run through the resulting records of the select query in the FormKeyPress procedure: (the query is not active at the start)
// first activate the query, so it executes on the server
SQLQuery2.Active := true;
SQLQuery2.First;
// run through the records
for IntI := 1 to (ord(Key) -48) * 1000 do
SQLQuery2.Next;
// check if we didn't run out of the table
If SqlQuery2.EOF
then
// Show a warning
showmessage('Sorry, we hit the bottom of the table...')
else
// Show the values of the textfield
showmessage(SQLQuery2.FieldByName('TESTTEXT').AsString);
// deactivate the query again
SQLQuery2.Active := false;
The code displays the value of the field with fieldname TESTTEXT in the current record after running through a number of records depending on the number pressed on the keyboard (1000 records times the number pressed).
2. Launchquery:
procedure LaunchQuery(QueryObj:TSqlQuery;TransActionObj:TSqlTransaction;StrSqlIn:string);
begin
TransActionObj.StartTransaction;
QueryObj.SQL.Clear;
QueryObj.SQL.Add(StrSqlIn);
QueryObj.ExecSQL;
TransActionObj.Commit;
end;
This procedure launches the query in StrSqlIn on QueryObj using transaction TransActionObj and directly after launching commits it on TransActionObj. So in this test for every query a transaction is started and committed.
3. Statements for deleting records in the tables:
This is used to delete the records from the Mysql and Firebird tables with the following statements:
//Delete all records from the Firebird table
StrSql:= 'delete from testtab;';
LaunchQuery(SQLQuery1, Sqltransaction1, StrSql);
//Delete all records from the Mysql table (same StrSql)
LaunchQuery(SQLQuery2, Sqltransaction2, StrSql);
4. Statements for inserting and updating:
In the loops records are inserted and updated with:
(SqlQuote is a single quote ( ' ) : const SqlQuote = #39;)
//Insert in the Mysql table
StrSql:= 'insert into testtab values(' + IntToStr(IntM) + ',' +
SqlQuote + 'a little test ' + IntToStr(IntM) + SqlQuote + ');';
LaunchQuery(SQLQuery2, Sqltransaction2, StrSql);
//Update the Mysql table
StrSql:= 'update testtab set testtext = ' + SqlQuote +
'another little test ' + IntToStr(IntM) + SqlQuote +
' where testnum = ' + IntToStr(IntM - 1);
LaunchQuery(SQLQuery2, Sqltransaction2, StrSql);
The same queries are used for the Firebird table, using SQLQuery1 and Sqltransaction1.
I Think this is more useful than just cutting and pasting the entire program,
Theo