3

I would like to take the following unit (DrivesData) and display the drive column in a TListView. I've never worked with the (Synopse) SQLite3 code before so I'm hoping someone could give me a little push in the right direction.

Just add the DrivesData unit to the uses clause then run and it will create the "drives.sqlite" database file with a list of drives 'A' to 'Z'.

unit DrivesData;

interface

uses
  SynCommons, SQLite3Commons;

type
  TDrives = class(TSQLRecord)
  private
    { Private declarations }
    FDrive: RawUTF8;
  protected
    { Protected declarations }
    FDrivesModel: TSQLModel;
    FDrivesDatabase: TSQLRest;
  public
    { Public declarations }
    constructor Create(); override;
    destructor Destroy(); override;
  published
    { Published declarations }
    property Drive: RawUTF8 read FDrive write FDrive;
  end;

var
  DriveRecord: TDrives;

implementation

uses
  SQLite3;

function CreateDrivesModel(): TSQLModel;
begin
  Result := TSQLModel.Create([TDrives]);
end;

{ TDrives }
constructor TDrives.Create();
var
  X: Char;
begin
  inherited Create();

  FDrivesModel := CreateDrivesModel();
  FDrivesDatabase := TSQLRestServerDB.Create(FDrivesModel, 'drives.sqlite');

  TSQLRestServerDB(FDrivesDatabase).DB.Execute(
    'CREATE TABLE IF NOT EXISTS drives ' +
    '(id INTEGER PRIMARY KEY, drive TEXT NOT NULL UNIQUE COLLATE NOCASE);');

  for X := 'A' to 'Z' do
  begin
    TSQLRestServerDB(FDrivesDatabase).DB.Execute(
      'INSERT OR IGNORE INTO drives (drive) VALUES ("' + X + ':")');
  end;
end;

destructor TDrives.Destroy();
begin
  if Assigned(FDrivesDatabase) then
    FDrivesDatabase.Free();

  if Assigned(FDrivesModel) then
    FDrivesModel.Free();

  inherited Destroy();
end;

initialization
  DriveRecord := TDrives.Create();

finalization
  if Assigned(DriveRecord) then
    DriveRecord.Free();

end.
2
  • 1
    Instead of adding 26 entries in a table, why not just add 26 items directly to a list view? Why involve a database at all? Commented May 23, 2011 at 13:57
  • 1
    Why? Basically, for educational purposes. Commented May 23, 2011 at 14:07

1 Answer 1

3

Nice try!

But I'm afraid you are missing some points of the framework:

  • for instance you're mixing record level and MVC application level: a TSQLRecord maps a DB table and you should not declare MVC TSQLModel and TSQLRest inside this class;
  • and you're missing the ORM approach, you don't need to write all those SQL code (the CREATE TABLE and the INSERT): the framework will write it for you, with no error, and the exact expected column type (with collations)!

Instead of using a TSQLRestServerDB directly by itself, you should better use a TSQLRestClientDB (which will instantiate its privately owned TSQLRestServerDB), even if you are still working locally. So you'll get a lot more features with no performance penalty.

You are using a Char type in your code. Our framework is UTF-8 oriented, so you should use AnsiChar instead, or use StringToUtf8() function to ensure correctness (at least with Unicode version of Delphi).

I'll recommend that you take a look at the sample code source code and the provided documentation (especially the SAD document, in the general presentation in the first pages, including the SynFile main demo).

To retrieve some data, then display it in the VCL (e.g. in a TListBox), take a look at the TSQLTableJSON class. There are some code sample in the SAD document (take a look at the keyword index, at the beginning of the document, if you're a bit lost).

Perhaps StackOverflow is not the best place to ask such specific questions. You have our forum available at http://synopse.info to post any questions regarding this framework. You can post your code here.

Thanks for your interest!

Sign up to request clarification or add additional context in comments.

1 Comment

I've written some more correct code in the forum, following your purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.