Corrections to the wiki (Solved) lazarus / 16-Databases ---------------------------------------------------------------------- dbannon · 2017-06-03 04:10 I am aware of a bit of a code example on the wiki that is, IMHO, a bit buggy. I'd like people's reassurance before I edit it. Not sure of the etiquette around here. I'm new here but, long, long ago, considered myself a reasonable Turbo Pascal and Delphi user. Recently needed a bit of code and remembered the FP/Lazaeus project. So, quickly found a good start on a bit of code called CreateTable on http://wiki.freepascal.org/SqlDBHowto but to my surprise, the code has some problems. I really like the idea of example code, something you can copy and paste, and one way or another, run. This code has no GUI, starts with a "program" declaration, ends with "end." and so looks like  standalone FP programme. But it does not compile because - 1. Is missing a function (noted in text). 2. Needs but has no "uses" clause. 3. Has a function returning a result via keyword 'result', OK in Lazarus but not FP. 4. Has badly constructed strings, using single inverted comma within the string, compiler thinks thats the end of the string. 5. Needs to free the Transaction before the Connection to avoid a run time error. I have edited the example a bit and produced a working version in about the same number of lines. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program CreateTable; uses  SysUtils, sqldb, db, sqlite3conn;  var   AConnection : TSQLite3Connection;  ATransaction : TSQLTransaction; begin  AConnection := TSQLite3Connection.Create(nil);  AConnection.DatabaseName := 'test_dbase';  // Thats all SQLite needs, other dbases might also want -  // AConnection.Hostname := 'localhost';  // AConnection.UserName := 'sysdba';  // AConnection.Password := 'masterkey';   ATransaction := TSQLTransaction.Create(AConnection);  AConnection.Transaction := ATransaction;  AConnection.Open;  ATransaction.StartTransaction;  AConnection.ExecuteDirect('create table TBLNAMES (ID integer, NAME varchar(40));');    ATransaction.Commit;   ATransaction.StartTransaction;  AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (1,''Name1'');');   AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (2,''Name2'');');   ATransaction.Commit;   AConnection.Close;  ATransaction.Free;      AConnection.Free;end. ---------------------------------------------------------------------- Thaddy · 2017-06-03 08:27 Some remarks if you want to improve on example code (good effort): You actually leave some issues open. ad 2: there is too much there ad 3: that depends on the mode: it is not a real issue. Simply add the proper mode And the code is not sufficiently safe. Try: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program CreateTable;{$mode objfpc} {$ifdef mswindows}{$apptype console}{$endif}uses  sqldb, sqlite3conn; var  AConnection : TSQLite3Connection;  ATransaction : TSQLTransaction; begin  AConnection := TSQLite3Connection.Create(nil);  try    AConnection.DatabaseName := 'test_dbase';    ATransaction := TSQLTransaction.Create(AConnection);    try      AConnection.Transaction := ATransaction;      AConnection.Open;      ATransaction.StartTransaction;      AConnection.ExecuteDirect('create table TBLNAMES (ID integer, NAME varchar(40));');      ATransaction.Commit;      ATransaction.StartTransaction;      AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (1,''Name1'');');      AConnection.ExecuteDirect('insert into TBLNAMES (ID,NAME) values (2,''Name2'');');      ATransaction.Commit;      AConnection.Close;    finally      ATransaction.Free;    end;  finally       AConnection.Free;  end;end. I would personally also add an if fileexists... And exception handling for database errors, but that is probably nitpicking in the context of an example ---------------------------------------------------------------------- wp · 2017-06-03 08:31 @dbannon: Do you know that everybody can edit the wiki? Just register and you are ready to go. But, of course, it is a good idea to present the intended changes here for discussion. Thank you. ---------------------------------------------------------------------- dbannon · 2017-06-04 02:18 Thanks WP, Thaddy. Yep, knew I could edit, just wanted to know if I should edit  I have bit of a training background. I think that block of code needs to be minimal, only include whats essential to compile and run.  But, obviously agree with Thaddy about error checking. So, what I'll do is - Replace existing code with my version (and include Thaddy's compiler directives). Add another block of code further down, after the section on reads, that does include some sensible error checking. There are already comments there about that need. So, creating a table, writing to it, reading from it and checking errors is a nice summary at that stage. ---------------------------------------------------------------------- wp · 2017-06-04 09:25 I could imagine presentation of code in the wiki in three ways: code fragment: just a few lines showing the essential ideas, will not necessarily compile. Not good for beginners because they may not know about the missing parts. But not everybody is a beginner... And the advantage is that the length of the article can be kept at a reasonable length.complete procedures: can be pasted into a program and will compile and run there. Necessary adjustments of "uses" and "requirements" will have to be made by the user. Still needs some experience.complete programs: should compile and run if pasted in to a separate file. Ideal for beginners, but will blow up the article length considerably.The code in the wiki page that we are talking about begins with "program", i.e. claims to belong to group 3. However, it does not compile and, therefore, should be changed. ---------------------------------------------------------------------- dbannon · 2017-06-04 10:49 OK, done. ---------------------------------------------------------------------- Thaddy · 2017-06-13 08:46 Quote from: dbannon on June 04, 2017, 12:49:27 pmOK, done. Ok, Well Done!