zoukankan      html  css  js  c++  java
  • delphi7连接mysql5一方法

    今天开始研究mysql,在网上搜了一下资料,都是要安装这个,安装那个,很麻烦。

    经过一直摸索、测试,得到一个快速的方法,很实用,只是稳定性有待发现。

    先去下载:http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html 

    然后把下载到的dbxopenmysql5_dll.zip解压出来,再把dbxopenmysql50.dll和libmysql.dll都放到工程文件夹下。

    在Form上放上TSQLConnection、TSQLQuery、TStringGrid、3个TButton、TLable。

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DBXpress, FMTBcd, StdCtrls, Grids, DB, SqlExpr;
    
    type
      TForm1 = class(TForm)
        SQLConnection1: TSQLConnection;
        SQLQuery1: TSQLQuery;
        StringGrid1: TStringGrid;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SQLConnection1 := TSQLConnection.Create(nil);
      SQLConnection1.DriverName := 'dbxmysql';
      SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50';
      SQLConnection1.LibraryName := 'dbxopenmysql50.dll';
      SQLConnection1.VendorLib := 'libmysql.dll';
      SQLConnection1.LoginPrompt := false;
      SQLConnection1.Params.Append('Database=mysql');
      SQLConnection1.Params.Append('User_Name=root');
      SQLConnection1.Params.Append('Password=');
      SQLConnection1.Params.Append('HostName=localhost');
      SQLConnection1.Open;
      if SQLConnection1.Connected = true then
      begin
        SQLQuery1.SQLConnection := SQLConnection1;
        SQLQuery1.SQL.Clear;
        SQLQuery1.SQL.Text := 'set names utf8'; //设置mysql查询中文不乱码
        SQLQuery1.ExecSQL();    
        Label1.Caption := 'success!';
        button1.Enabled := False;
        Button3.Enabled := True;
      end else
      begin
        Label1.Caption := 'failed!';
        SQLConnection1.Close;
      end;
    end;
    
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      i, j: Integer;
    begin   
      SQLQuery1.SQL.Clear;
      SQLQuery1.SQL.Add('SELECT * FROM user');
      SQLQuery1.Active := true;
      i := 0;
      SQLQuery1.First;
      while not SQLQuery1.eof do
      begin
        for j := 0 to SQLQuery1.FieldCount - 1 do
          StringGrid1.cells[j, i] := SQLQuery1.Fields[j].AsString;
        SQLQuery1.next;
        inc(i);
      end;
      SQLQuery1.Active := false;
    end;
    
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      if SQLConnection1.Connected = true then
        SQLConnection1.Close;
      SQLConnection1.Free;
    end;
    
    end.
    

      经测试,连接OK,查询OK。

        参考:JackSun's 技术博客 http://www.cnblogs.com/JackSun/archive/2010/12/16/1908145.html

        参考:liuweijie 技术博客  https://www.cnblogs.com/weijie-liu/p/9644635.html

  • 相关阅读:
    webpack前端构建工具学习总结(一)之webpack安装、创建项目
    当执行 import vue from 'vue' 时发生了什么?
    WEBSTORM新建VUE类型文件设置
    基于vue-cli搭建HelloWorld项目
    vue-cli脚手架安装
    浏览器的重绘和回流
    strcpy和strncpy用法和区别
    &与&&有什么区别?
    状态机实践入门
    12864点阵液晶显示模块的原理和实例程序(HJ12864M-1)
  • 原文地址:https://www.cnblogs.com/evil39c/p/3810624.html
Copyright © 2011-2022 走看看