首页 > 技术文章 > (转载)Delphi7中ZEOSDBO控件使用 原文 https://www.amingstudio.com/delphi/225.html

honeynm 2021-04-02 10:57 原文

本文采用的是Delphi7+ZEOSDBO

 
查看更多关于 ZEOSDBO 的文章

控件,读取Sqlite数据库,并解决Dbgrid Memo字段及乱码的问题

很多同学 都在问Delphi7怎么弄sqlite数据库, 网上也没有很清晰的,所以就写出来,大家一起分享
本人菜鸟,大神勿喷。
这里使用的是ZEOSDBO控, 官网为https://sourceforge.net/projects/zeoslib/,最新版本为7.2.4。
支持的很多。。。
(pooled.* ASA7 ASA8 ASA9 ASA12 oracle oracle-9i sqlite sqlite-3 interbase-6
firebird-1.0 firebird-1.5 firebird-2.0 firebird-2.1 firebird-2.5 firebirdd-1.5
firebirdd-2.0 firebirdd-2.1 firebirdd-2.5 postgresql postgresql-7 postgresql-8 postgresql-9
mysql mysql-4.1 mysql-5 mysqld-4.1 mysqld-5 MariaDB-5 mssql sybase FreeTDS_MsSQL<=6.5 FreeTDS_Sybase<10 FreeTDS_Sybase-10+ FreeTDS_MsSQL-7.0 FreeTDS_MsSQL-2000 FreeTDS_MsSQL>=2005 ado)

一、安装控件

    • 1、进入 ...\ZEOSDBO-7.1.4-stable\packages\delphi7 双击 ZeosDbo.bpg。然后点击Delphi7菜单栏的 project à compile all projects,编译完点击 ok。
    • 2、在Delphi7菜单栏 Tool àEnvironmentOptionsàLibraryàLibrary Path中加入路径,(所在文件夹)\ZEOSDBO-7.1.4-stable\packages\delphi7\build
 
  • 3、双击 ..\ZEOSDBO-7.1.4-stable\packages\delphi7\ZComponentDesign.dpk,点击install,安装完毕后,控件就都出现了

二、读取数据库

    • 1、新建个application 拖入DbGrid1、DataSource1、ZQuery1、ZConnection1、listview1、Button1、Button2 7个控件
    • 2、首先把控件链接起来,我习惯用代码写在Form1的onCreate事件中
      procedure TForm1.FormCreate(Sender: TObject);
      var
        path: string;
      begin
        path := ExtractFileDir(ParamStr(0)); // 获取根目录路径
        ZQuery1.Connection := ZConnection1;
        DataSource1.DataSet := ZQuery1;
        DBGrid1.DataSource := DataSource1;
        with ZConnection1 do
        begin
          Protocol := 'sqlite-3'; // 数据库类型
          port := 3306; // 端口
          HostName := '127.0.0.1';
          Database := path + '/FpcEfficiency.db'; // 数据库路径
          Connected := True;
        end;
 
    • 3、在Listview1中显示表名
      • 1)修改Listview1的 ViewStyle属性为 vsReport ,双击Listview1 添加一个column 修改Caption属性为 数据库表名,RowSelect属性改为True,GridLines属性改为True;
      • 2)修改button2 caption属性为 读取表 button1 Caption属性为连接表
      • 3)在Button2的OnClick事件中写入代码
      procedure TForm1.Button2Click(Sender: TObject);
      var
        s: TStrings;
        i: Integer;
      begin
        ListView1.Items.Clear;
        try
          s := Tstringlist.Create;
          ZConnection1.GetTableNames('', s); // 第一个参数为过滤 不过滤为空
          for i := 0 to s.Count - 1 do
          begin
            ListView1.Items.Add.Caption := s.Strings[i];
          end;
        finally
          FreeAndNil(s);
        end;
      end;

      这时,已经能读取表名了

 
    • 4、在DbGrid1中显示数据库内容
      在Button1的OnClick事件中写入代码

       

      procedure TForm1.Button1Click(Sender: TObject);
      begin
        if ListView1.Items.Count = 0 then
          ShowMessage('请先读取表名')
        else if ListView1.SelCount <= 0 then
          ShowMessage('先选择表!')
        else
        begin
          with ZQuery1 do
          begin
            Close;
            SQL.Clear;
            SQL.add('select * from ' + ListView1.Selected.Caption);
            Open;
          end;
        end;
      end;

      这时若发现全部为乱码,修改ZConnection1的AutoEncodeString属性为True就好了。

 
  • 5、由于用的是Dbgrid,Text字段都不显示内容,显示的为(Memo)。网上有很多解决的办法,大部分都推荐换DbGridh控件,这里既然用,就不换了,用代码搞定。
    Query1 Fields 有个ongettext事件,用这个实现。定义两个过程

     

    procedure TForm1.ShowTextField(Sender: TField; var Text: string;
      DisplayText: Boolean);
    begin
      Text := Sender.AsString;
    end;
    
    procedure TForm1.SetTextFieldEvent(const ADatSet: TZQuery);
    var
      i: integer;
    begin
      with ADatSet do
      begin
        DisableControls;
        try
          for i := 0 to Fields.Count - 1 do
            if Fields[i] is TMemoField then
              Fields[i].OnGetText := ShowTextField;
        finally
          EnableControls;
        end;
      end;
    end;

    然后在ZQuery1的AfterOpen事件中写入代码

    procedure TForm1.ZQuery1AfterOpen(DataSet: TDataSet);
    begin
     SetTextFieldEvent(ZQuery1);
    end;

三、最后就是DbGrid1颜色调整了。在DbGrid1控件的onDrawColumnCell事件中自己发挥了

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  i: Integer;
begin
  if gdSelected in State then
    Exit;
  // 定义表头的字体和背景颜色:
  for i := 0 to (Sender as TDBGrid).Columns.Count - 1 do
  begin
    (Sender as TDBGrid).Columns[i].Title.Font.Name := '宋体'; // 字体
    (Sender as TDBGrid).Columns[i].Title.Font.Size := 9; // 字体大小
    (Sender as TDBGrid).Columns[i].Title.Font.Color := $000000FF; // 字体颜色(红色)
    (Sender as TDBGrid).Columns[i].Title.Color := $0000FF00; // 背景色(绿色)
  end;
  // 隔行改变网格背景色:
  if zQuery1.RecNo mod 2 = 0 then
    (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk // 定义背景颜色
  else
    (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); // 定义背景颜色
  // 定义网格线的颜色:
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  with (Sender as TDBGrid).Canvas do // 画 cell 的边框
  begin
    Pen.Color := $00FF0000; // 定义画笔颜色(蓝色)
    MoveTo(Rect.Left, Rect.Bottom); // 画笔定位
    LineTo(Rect.Right, Rect.Bottom); // 画蓝色的横线
    Pen.Color := $0000FF00; // 定义画笔颜色(绿色)
    MoveTo(Rect.Right, Rect.Top); // 画笔定位
    LineTo(Rect.Right, Rect.Bottom); // 画绿色的竖线
  end;
end;

结果如图所示
Delphi7中ZEOSDBO控件使用 Delphi专题 第1张

推荐阅读