简体   繁体   English

在Delphi7 TListView中显示(Synopse)SQLite3表列

[英]Display a (Synopse) SQLite3 table-column in a Delphi7 TListView

I would like to take the following unit (DrivesData) and display the drive column in a TListView. 我想采用以下单元(DrivesData)并在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. 我以前从未使用过(Synopse)SQLite3代码,所以我希望有人能给我一点正确的方向。

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'. 只需将DrivesData单元添加到uses子句然后运行,它将创建“drives.sqlite”数据库文件,其中包含驱动器“A”到“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.

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; 例如,您正在混合记录级别和MVC应用程序级别: TSQLRecord映射数据库表,您不应在此类中声明MVC TSQLModelTSQLRest ;
  • 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)! 并且您错过了ORM方法,您不需要编写所有这些SQL代码(CREATE TABLE和INSERT):框架将为您编写,没有错误,以及确切的预期列类型(具有排序规则) )!

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. 而不是使用的TSQLRestServerDB直接通过本身,你最好使用TSQLRestClientDB (这将实例的私有TSQLRestServerDB ),即使你仍然在本地工作。 So you'll get a lot more features with no performance penalty. 因此,您将获得更多功能,而不会降低性能。

You are using a Char type in your code. 您在代码中使用Char类型。 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). 我们的框架是面向UTF-8的,所以你应该使用AnsiChar,或者使用StringToUtf8()函数来确保正确性(至少使用Unicode版本的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). 我建议您查看示例代码源代码和提供的文档 (特别是SAD文档,在第一页的一般演示文稿中,包括SynFile主演示)。

To retrieve some data, then display it in the VCL (eg in a TListBox ), take a look at the TSQLTableJSON class. 要检索某些数据,然后在VCL中显示它(例如在TListBox ),请查看TSQLTableJSON类。 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). SAD文档中有一些代码示例(如果您有点丢失,请查看文档开头的关键字索引)。

Perhaps StackOverflow is not the best place to ask such specific questions. 也许StackOverflow不是提出这些具体问题的最佳场所。 You have our forum available at http://synopse.info to post any questions regarding this framework. 您可以在http://synopse.info上找到我们的论坛,以发布有关此框架的任何问题。 You can post your code here. 您可以在此处发布您的代码。

Thanks for your interest! 谢谢你的关注!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM