delphi mysql数据库开发,Delphi MySQL数据库操作类

{* *MySQLHelperv1.0 *2015.6.19 *说明: *这是一个操作MySQL的类,该类必须和libmysql.dll,dbxmys.dll两个文件一起使用. *安装: *将dll拷贝到C:\Windows\System32下和项目目录下,发行的时候放到exe目录下即可. *使用: *//使用insert,update,delete语句时请使用

{*

*                         MySQL Helper v1.0

*                            2015.6.19

* 说明:

*     这是一个操作MySQL的类,该类必须和libmysql.dll,dbxmys.dll两个文件一起使用.

* 安装:

*     将dll拷贝到C:\Windows\System32下和项目目录下,发行的时候放到exe目录下即可.

* 使用:

*    //使用insert,update,delete语句时请使用:TMySQLHelper.ExecSQL();返回受影响行数Integer;

*    //使用select语句时请使用TMySQLHelper.Query();返回数据集TSQLQuery;

* 测试:

*     WIN7 SP1 X86 , MySQL 5.6.17 , Delphi XE 测试通过.

* ==========================================

* var

*   MySQLHelper : TMySQLHelper;

* begin

*   MySQLHelper :=  TMySQLHelper.Create;

*   MySQLHelper.User_name := 'root';

*   MySQLHelper.Password  := 'root';

*   MySQLHelper.Database  := 'Test';

*   ShowMessage('影响行数:'+IntToStr(MySQLHelper.ExecSQL('INSERT INTO test(name)values(''FangJun'')')));

*   MySQLHelper.Free;

* end;

* ==========================================

* var

*   MySQLHelper : TMySQLHelper;

*   SQLQuery : TSQLQuery;

* begin

*   MySQLHelper :=  TMySQLHelper.Create;

*   MySQLHelper.User_name := 'root';

*   MySQLHelper.Password  := 'root';

*   MySQLHelper.Database  := 'Test';

*   SQLQuery := TSQLQuery.Create(nil);

*   SQLQuery := MySQLHelper.Query('select * from test');

*   while not SQLQuery.Eof do

*     begin

*       ShowMessage('姓名:'+VarToStr(SQLQuery.FieldValues['name']);

*       SQLQuery.Next;

*     end;

*   MySQLHelper.MySQLClose;

*   MySQLHelper.Free;

* end;

* ==========================================

}

下载地址   http://files.cnblogs.com/files/fangjunai/MySQLHelper.rar

{*

* MySQL Helper v1.0

* 2015.6.19

* 说明:

* 这是一个操作MySQL的类,该类必须和libmysql.dll,dbxmys.dll两个文件一起使用.

* 安装:

* 将dll拷贝到C:\Windows\System32下和项目目录下,发行的时候放到exe目录下即可.

* 使用:

* //使用insert,update,delete语句时请使用:TMySQLHelper.ExecSQL();返回受影响行数Integer;

* //使用select语句时请使用TMySQLHelper.Query();返回数据集TSQLQuery;

* 测试:

* WIN7 SP1 X86 , MySQL 5.6.17 , Delphi XE 测试通过.

* ==========================================

* var

* MySQLHelper : TMySQLHelper;

* begin

* MySQLHelper := TMySQLHelper.Create;

* MySQLHelper.User_name := 'root';

* MySQLHelper.Password := 'root';

* MySQLHelper.Database := 'Test';

* ShowMessage('影响行数:'+IntToStr(MySQLHelper.ExecSQL('INSERT INTO test(name)values(''FangJun'')')));

* MySQLHelper.Free;

* end;

* ==========================================

* var

* MySQLHelper : TMySQLHelper;

* SQLQuery : TSQLQuery;

* begin

* MySQLHelper := TMySQLHelper.Create;

* MySQLHelper.User_name := 'root';

* MySQLHelper.Password := 'root';

* MySQLHelper.Database := 'Test';

* SQLQuery := TSQLQuery.Create(nil);

* SQLQuery := MySQLHelper.Query('select * from test');

* while not SQLQuery.Eof do

* begin

* ShowMessage('姓名:'+VarToStr(SQLQuery.FieldValues['name']);

* SQLQuery.Next;

* end;

* MySQLHelper.MySQLClose;

* MySQLHelper.Free;

* end;

* ==========================================

}

unit MySQLHelper;

interface

uses

SysUtils,StdCtrls,Classes,Variants,DB,SqlExpr,DBXMySQL;

type

TMySQLHelper = class(TObject)

private

_PORT : Integer;

_HOST : string;

_DATABASE : string;

_USER_NAME : string;

_PASSWORD : string;

_SERVERCHARSET : string;

_SQLQuery : TSQLQuery;

_SQLConnection : TSQLConnection;

procedure Set_PORT(const Value: Integer);

procedure Set_HOST(const Value: string);

procedure Set_DATABASE (const Value: string);

procedure Set_USER_NAME(const Value: string);

procedure Set_PASSWORD (const Value: string);

procedure Set_SERVERCHARSET(const Value: string);

function MySQLConnection:TSQLConnection;

public

constructor Create; overload;

property Post:Integer write Set_PORT;

property Host:string write Set_HOST;

property Database:string write Set_DATABASE;

property User_name:string write Set_USER_NAME;

property Password:string write Set_PASSWORD;

property ServerCharSet:string write Set_SERVERCHARSET;

function ExecSQL(const SQL:string):Integer;

function Query(const SQL:string):TSQLQuery;

procedure MySQLClose;

end;

implementation

//初始化

constructor TMySQLHelper.Create;

begin

_HOST := '127.0.0.1';

_PORT := 3306;

_SERVERCHARSET := 'utf8';

end;

//执行 SQL 语句 INSERT , UPDATE , DELETE 返回影响行数

function TMySQLHelper.ExecSQL(const SQL:string):Integer;

begin

if not Assigned(_SQLQuery) then

_SQLQuery := TSQLQuery.Create(nil);

with _SQLQuery do

begin

Close;

SQL.Clear;

SQLConnection := MySQLConnection;

end;

try

_SQLQuery.SQL.Add(SQL);

result := _SQLQuery.ExecSQL;

except on E: Exception do

raise Exception.Create('SQL语句执行失败 :'+E.Message);

end;

MySQLClose;

end;

//执行 SQL 语句 Select 返回 数据集

function TMySQLHelper.Query(const SQL:string):TSQLQuery;

begin

if not Assigned(_SQLQuery) then

_SQLQuery := TSQLQuery.Create(nil);

with _SQLQuery do

begin

Close;

SQL.Clear;

SQLConnection := MySQLConnection;

end;

try

_SQLQuery.SQL.Add(SQL);

_SQLQuery.Open;

_SQLQuery.Active := true;

result := _SQLQuery;

except on E: Exception do

raise Exception.Create('SQL语句查询失败 :'+E.Message);

end;

end;

//关闭连接

procedure TMySQLHelper.MySQLClose;

begin

_SQLQuery.Close;

_SQLConnection.Close;

end;

//连接MySQL 返回 TSQLConnection

function TMySQLHelper.MySQLConnection:TSQLConnection;

begin

if not Assigned(_SQLConnection) then

_SQLConnection := TSQLConnection.Create(nil);

with _SQLConnection do

begin

Close;

GetDriverFunc := 'getSQLDriverMYSQL';

LibraryName := 'dbxmys.dll';

VendorLib := 'LIBMYSQL.dll';

DriverName:= 'MySQL';

Params.Values['drivername']:= 'MySQL';

Params.Values['port'] := IntToStr(_PORT);

Params.Values['hostname'] := _HOST;

Params.Values['database'] := _DATABASE;

Params.Values['user_name'] := _USER_NAME;

Params.Values['password'] := _PASSWORD;

Params.Values['ServerCharSet'] := _SERVERCHARSET;

end;

try

_SQLConnection.Open;

_SQLConnection.Connected := true;

result := _SQLConnection;

except on E: Exception do

raise Exception.Create('数据库连接错误:'+E.Message);

end;

end;

procedure TMySQLHelper.Set_PORT(const Value: Integer);

begin

if Value<>0 then

_PORT := Value

end;

procedure TMySQLHelper.Set_HOST (const Value: string);

begin

if Value<>'' then

_HOST := Value

end;

procedure TMySQLHelper.Set_DATABASE (const Value: string);

begin

_DATABASE := Value

end;

procedure TMySQLHelper.Set_USER_NAME (const Value: string);

begin

_USER_NAME := Value;

end;

procedure TMySQLHelper.Set_PASSWORD (const Value: string);

begin

_PASSWORD := Value;

end;

procedure TMySQLHelper.Set_SERVERCHARSET (const Value: string);

begin

if Value<>'' then

_SERVERCHARSET := Value

end;

end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值