程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi XE MySQL數據庫操作類 MySQLHelper,xemysqlhelper

Delphi XE MySQL數據庫操作類 MySQLHelper,xemysqlhelper

編輯:Delphi

Delphi XE MySQL數據庫操作類 MySQLHelper,xemysqlhelper


  注: 無需odbc配置

1 {* 2 * MySQL Helper v1.0 3 * 2015.6.19 4 * 說明: 5 * 這是一個操作MySQL的類,該類必須和libmysql.dll,dbxmys.dll兩個文件一起使用. 6 * 安裝: 7 * 將dll拷貝到C:\Windows\System32下和項目目錄下,發行的時候放到exe目錄下即可. 8 * 使用: 9 * //使用insert,update,delete語句時請使用:TMySQLHelper.ExecSQL();返回受影響行數Integer; 10 * //使用select語句時請使用TMySQLHelper.Query();返回數據集TSQLQuery; 11 * 測試: 12 * WIN7 SP1 X86 , MySQL 5.6.17 , Delphi XE 測試通過. 13 * ========================================== 14 * var 15 * MySQLHelper : TMySQLHelper; 16 * begin 17 * MySQLHelper := TMySQLHelper.Create; 18 * MySQLHelper.User_name := 'root'; 19 * MySQLHelper.Password := 'root'; 20 * MySQLHelper.Database := 'Test'; 21 * ShowMessage('影響行數:'+IntToStr(MySQLHelper.ExecSQL('INSERT INTO test(name)values(''FangJun'')'))); 22 * MySQLHelper.Free; 23 * end; 24 * ========================================== 25 * var 26 * MySQLHelper : TMySQLHelper; 27 * SQLQuery : TSQLQuery; 28 * begin 29 * MySQLHelper := TMySQLHelper.Create; 30 * MySQLHelper.User_name := 'root'; 31 * MySQLHelper.Password := 'root'; 32 * MySQLHelper.Database := 'Test'; 33 * SQLQuery := TSQLQuery.Create(nil); 34 * SQLQuery := MySQLHelper.Query('select * from test'); 35 * while not SQLQuery.Eof do 36 * begin 37 * ShowMessage('姓名:'+VarToStr(SQLQuery.FieldValues['name']); 38 * SQLQuery.Next; 39 * end; 40 * MySQLHelper.MySQLClose; 41 * MySQLHelper.Free; 42 * end; 43 * ========================================== 44 } 45 unit MySQLHelper; 46 47 interface 48 49 uses 50 SysUtils,StdCtrls,Classes,Variants,DB,SqlExpr,DBXMySQL; 51 52 type 53 TMySQLHelper = class(TObject) 54 private 55 _PORT : Integer; 56 _HOST : string; 57 _DATABASE : string; 58 _USER_NAME : string; 59 _PASSWORD : string; 60 _SERVERCHARSET : string; 61 _SQLQuery : TSQLQuery; 62 _SQLConnection : TSQLConnection; 63 64 procedure Set_PORT(const Value: Integer); 65 procedure Set_HOST(const Value: string); 66 procedure Set_DATABASE (const Value: string); 67 procedure Set_USER_NAME(const Value: string); 68 procedure Set_PASSWORD (const Value: string); 69 procedure Set_SERVERCHARSET(const Value: string); 70 function MySQLConnection:TSQLConnection; 71 72 public 73 constructor Create; overload; 74 property Post:Integer write Set_PORT; 75 property Host:string write Set_HOST; 76 property Database:string write Set_DATABASE; 77 property User_name:string write Set_USER_NAME; 78 property Password:string write Set_PASSWORD; 79 property ServerCharSet:string write Set_SERVERCHARSET; 80 81 function ExecSQL(const SQL:string):Integer; 82 function Query(const SQL:string):TSQLQuery; 83 procedure MySQLClose; 84 end; 85 86 implementation 87 88 //初始化 89 constructor TMySQLHelper.Create; 90 begin 91 _HOST := '127.0.0.1'; 92 _PORT := 3306; 93 _SERVERCHARSET := 'utf8'; 94 end; 95 96 //執行 SQL 語句 INSERT , UPDATE , DELETE 返回影響行數 97 function TMySQLHelper.ExecSQL(const SQL:string):Integer; 98 begin 99 if not Assigned(_SQLQuery) then 100 _SQLQuery := TSQLQuery.Create(nil); 101 with _SQLQuery do 102 begin 103 Close; 104 SQL.Clear; 105 SQLConnection := MySQLConnection; 106 end; 107 try 108 _SQLQuery.SQL.Add(SQL); 109 result := _SQLQuery.ExecSQL; 110 except on E: Exception do 111 raise Exception.Create('SQL語句執行失敗 :'+E.Message); 112 end; 113 MySQLClose; 114 end; 115 116 //執行 SQL 語句 Select 返回 數據集 117 function TMySQLHelper.Query(const SQL:string):TSQLQuery; 118 begin 119 if not Assigned(_SQLQuery) then 120 _SQLQuery := TSQLQuery.Create(nil); 121 with _SQLQuery do 122 begin 123 Close; 124 SQL.Clear; 125 SQLConnection := MySQLConnection; 126 end; 127 try 128 _SQLQuery.SQL.Add(SQL); 129 _SQLQuery.Open; 130 _SQLQuery.Active := true; 131 result := _SQLQuery; 132 except on E: Exception do 133 raise Exception.Create('SQL語句查詢失敗 :'+E.Message); 134 end; 135 end; 136 137 //關閉連接 138 procedure TMySQLHelper.MySQLClose; 139 begin 140 _SQLQuery.Close; 141 _SQLConnection.Close; 142 end; 143 144 //連接MySQL 返回 TSQLConnection 145 function TMySQLHelper.MySQLConnection:TSQLConnection; 146 begin 147 if not Assigned(_SQLConnection) then 148 _SQLConnection := TSQLConnection.Create(nil); 149 with _SQLConnection do 150 begin 151 Close; 152 GetDriverFunc := 'getSQLDriverMYSQL'; 153 LibraryName := 'dbxmys.dll'; 154 VendorLib := 'LIBMYSQL.dll'; 155 DriverName:= 'MySQL'; 156 Params.Values['drivername']:= 'MySQL'; 157 Params.Values['port'] := IntToStr(_PORT); 158 Params.Values['hostname'] := _HOST; 159 Params.Values['database'] := _DATABASE; 160 Params.Values['user_name'] := _USER_NAME; 161 Params.Values['password'] := _PASSWORD; 162 Params.Values['ServerCharSet'] := _SERVERCHARSET; 163 end; 164 try 165 _SQLConnection.Open; 166 _SQLConnection.Connected := true; 167 result := _SQLConnection; 168 except on E: Exception do 169 raise Exception.Create('數據庫連接錯誤:'+E.Message); 170 end; 171 end; 172 173 174 procedure TMySQLHelper.Set_PORT(const Value: Integer); 175 begin 176 if Value<>0 then 177 _PORT := Value 178 end; 179 180 procedure TMySQLHelper.Set_HOST (const Value: string); 181 begin 182 if Value<>'' then 183 _HOST := Value 184 end; 185 186 procedure TMySQLHelper.Set_DATABASE (const Value: string); 187 begin 188 _DATABASE := Value 189 end; 190 191 procedure TMySQLHelper.Set_USER_NAME (const Value: string); 192 begin 193 _USER_NAME := Value; 194 end; 195 196 procedure TMySQLHelper.Set_PASSWORD (const Value: string); 197 begin 198 _PASSWORD := Value; 199 end; 200 201 procedure TMySQLHelper.Set_SERVERCHARSET (const Value: string); 202 begin 203 if Value<>'' then 204 _SERVERCHARSET := Value 205 end; 206 207 end.

 下載地址 MySQLHelper

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved