gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:47 25 4
gpt4 key购买 nike

我想采用以下单元 (DrivesData) 并在 TListView 中显示驱动器列。我以前从未使用过 (Synopse) SQLite3 代码,所以我希望有人能在正确的方向上给我一点插入。

只需将 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.

最佳答案

不错的尝试!

但恐怕您遗漏了框架的一些要点:

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

与其直接单独使用 TSQLRestServerDB,不如使用 TSQLRestClientDB(它将实例化其私有(private)的 TSQLRestServerDB),即使如果你还在本地工作。因此,您将获得更多功能而不会降低性能。

您在代码中使用了 Char 类型。我们的框架是面向 UTF-8 的,因此您应该改用 AnsiChar,或使用 StringToUtf8() 函数来确保正确性(至少对于 Unicode 版本的 Delphi)。

我建议您查看示例代码源代码和 provided documentation (特别是SAD文档,在第一页的一般介绍,包括SynFile main demo)。

要检索一些数据,然后将其显示在 VCL 中(例如,在 TListBox 中),请查看 TSQLTableJSON 类。 SAD 文档中有一些代码示例(如果您有点迷路,请查看文档开头的关键字索引)。

也许 StackOverflow 不是提出此类具体问题的最佳场所。您可以在 http://synopse.info 访问我们的论坛发布有关此框架的任何问题。您可以在此处发布您的代码。

感谢您的关注!

关于database - 在 Delphi7 TListView 中显示 (Synopse) SQLite3 表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6098096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com