delphi7连接mysql5一方法

摘要:
今天,我开始学习MySQL。我在网上搜索信息,发现安装这个和那个非常麻烦。通过不断的探索和测试,获得了一种快速的方法,这是非常实用的,但稳定性仍有待发现。先下载:http://www.justsoftwaresolutions.co.uk/delphi/dbexpress_and_mysql_5.html然后下载dbxopenmysql5_解压dll。zip,然后将dbxopenmysql50.dll和libmysqlDLL放在项目文件夹下。将TSQLConnection、TSQLQuery、TStringGrid、三个TButton和TLable放在表单.unitUnit1上;界面使用Windows、消息、SysUtils、变量、类、图形、控件、窗体、对话框、DBXpress、FMTBcd、StdCtrls、网格、DB、SqlExpr;typeTForm1=classSQLConnection1:TSQLConnection;SQLQuery1:TSQLQuery;StringGrid1:TStringGrid;按钮1:T按钮;按钮2:T按钮;按钮3:T按钮;标签1:TLabel;程序按钮单击;程序按钮2Click;procedureButton3单击;私有{私有声明}公共{公共声明}结束;varForm1:TForm1;实现{$R*.dfm}过程TForm1.Button1Click;beginSQLConnection1:=TSQL连接。创造SQLConnection1.DriverName:='dbxmysql';SQLConnection1.GetDriverFunc:='getSQLDriverMYSQL50';SQLConnection1.LibraryName:='dbxopenmysql50.dll';SQLConnection1.VendorLib:='libmysql。dll';SQLConnection1.LoginPmpt:=false;SQLConnection1.Params。追加SQLConnection1.Params。追加SQLConnection1.Params。追加SQLConnection1.Params。追加SQLConnection1.Open;如果SQLConnection1.Connected=true则开始SQLQuery1.SQLConnection:=SQLConnection1;SQLQuery1.SQL。清楚的SQLQuery1.SQL。文本:='setnamesutf8';//设置MySQL以查询中文非乱码SQLQuery1.ExecSQL();标签1.Caption:=成功!

今天开始研究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

免责声明:文章转载自《delphi7连接mysql5一方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Fedora 28 报 error while loading shared libraries: libtinfo.so.5 解决办法[小程序]微信小程序获取input并发送网络请求下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C/C++迭代器使用具体解释

迭代器是一种检查容器内元素并遍历元素的数据类型。能够替代下标訪问vector对象的元素。 每种容器类型都定义了自己的迭代器类型,如 vector: vector<int>::iterator iter;这符语句定义了一个名为 iter 的变量。它的数据类型是 vector<int> 定义的 iterator 类型。每一个标准库容器...

STM32CubeIDE+FreeRTOS消息队列实验

新建工程RTOS_Message,配置如下: Ctrl + S生成代码 修改代码, 1,在main.h中添加 //添加include/*Private includes ----------------------------------------------------------*/ /*USER CODE BEGIN Includes */#in...

动态将ADOQuery数据移植到ClientDataSet通用函数

昨天做程序突然想到的,如果您会使用内存表就不需要此例了,可以参考一下,代码如下: //******************************************************************************// 函数功能: 执行SQL查询语句,影响ClientDataSet控件// 函数名称: cf_dbSelectSQ...

SVG SMIL animation动画详解

一、SVG SMIL animation概览 1. SMIL是什么? SMIL不是指「水蜜梨」,而是Synchronized Multimedia Integration Language(同步多媒体集成语言)的首字母缩写简称,是有标准的。本文所要介绍的SVG动画就是基于这种语言。 SMIL允许你做下面这些事情: 动画元素的数值属性(X, Y, …) 动...

触发器实现多表之间的增加、删除及更新

注:本文参考http://blog.sina.com.cn/s/blog_a0912d340101gxhb.html 常见的触发器有三种:分别应用于Insert,Update,Delete事件。 1.数据同步增加:如有两张表:A表和B表,创建触发器使当A表插入数据后B表也同步插入数据。其中B表插入数据的字段需要同A表中的字段相对应。 1 create tr...

SQl Server Tsql基本编程 ,循环语句 ,存储过程

一些比较重要但是不一定经常用的 句子 Tsql定义变量 declare @a int ; 定义的变量前面必须用@,数据类型是SQL里的数据类型,执行的时候要把需要的有关联的代码一起执行,单独执行一条没有用。 赋值 set @a=‘aaa’; select @a=‘aaa’; 这两种都可以进行赋值, select @a 可以直接打印出a的值。 print@a...