DELPHI XE MYSQL数据库操作类 MYSQLHELPER


本文整理自网络,侵删。

 
注: 无需odbc配置
{*
 *                         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.

相关阅读 >>

Delphi 检测文件是否被占用

Delphi搜索字符串在流中的位置

Delphi获得系统当前时间日期和格式化时间

Delphi tdbgrideh导出excel

Delphi 模拟网站验证码

Delphi中转向语句break,continue,exit的作用

Delphi 注入不用writeprocessmemory 函数

Delphi 创建圆形或椭圆形窗体

shellexecute的多种用法

Delphi调用http接口上传文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...