本篇文章主要介绍了"Delphi XE MySQL数据库操作类 MySQLHelper",主要涉及到方面的内容,对于Delphijrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
注: 无需odbc配置 1{*2 * MySQL Helper v1.03 * ...
注: 无需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} 45unit MySQLHelper;
46 47interface
48 49uses
50 SysUtils,StdCtrls,Classes,Variants,DB,SqlExpr,DBXMySQL;
51 52type 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 64procedure Set_PORT(const Value: Integer);
65procedure Set_HOST(const Value: string);
66procedure Set_DATABASE (const Value: string);
67procedure Set_USER_NAME(const Value: string);
68procedure Set_PASSWORD (const Value: string);
69procedure Set_SERVERCHARSET(const Value: string);
70function 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 81function ExecSQL(const SQL:string):Integer;
82function Query(const SQL:string):TSQLQuery;
83procedure MySQLClose;
84end;
85 86implementation
87 88 //初始化
89 constructor TMySQLHelper.Create;
90begin 91 _HOST := '127.0.0.1';
92 _PORT := 3306;
93 _SERVERCHARSET := 'utf8';
94end;
95 96 //执行 SQL 语句 INSERT , UPDATE , DELETE 返回影响行数
97function TMySQLHelper.ExecSQL(const SQL:string):Integer;
98begin 99ifnot Assigned(_SQLQuery) then100 _SQLQuery := TSQLQuery.Create(nil);
101with _SQLQuery do102begin103 Close;
104 SQL.Clear;
105 SQLConnection := MySQLConnection;
106end;
107 try
108 _SQLQuery.SQL.Add(SQL);
109 result := _SQLQuery.ExecSQL;
110 except on E: Exception do111 raise Exception.Create('SQL语句执行失败 :'+E.Message);
112end;
113 MySQLClose;
114end;
115116 //执行 SQL 语句 Select 返回 数据集
117function TMySQLHelper.Query(const SQL:string):TSQLQuery;
118begin119ifnot Assigned(_SQLQuery) then120 _SQLQuery := TSQLQuery.Create(nil);
121with _SQLQuery do122begin123 Close;
124 SQL.Clear;
125 SQLConnection := MySQLConnection;
126end;
127 try
128 _SQLQuery.SQL.Add(SQL);
129 _SQLQuery.Open;
130 _SQLQuery.Active := true;
131 result := _SQLQuery;
132 except on E: Exception do133 raise Exception.Create('SQL语句查询失败 :'+E.Message);
134end;
135end;
136137 //关闭连接
138procedure TMySQLHelper.MySQLClose;
139begin140 _SQLQuery.Close;
141 _SQLConnection.Close;
142end;
143144 //连接MySQL 返回 TSQLConnection
145function TMySQLHelper.MySQLConnection:TSQLConnection;
146begin147ifnot Assigned(_SQLConnection) then148 _SQLConnection := TSQLConnection.Create(nil);
149with _SQLConnection do150begin151 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;
163end;
164 try
165 _SQLConnection.Open;
166 _SQLConnection.Connected := true;
167 result := _SQLConnection;
168 except on E: Exception do169 raise Exception.Create('数据库连接错误:'+E.Message);
170end;
171end;
172173174procedure TMySQLHelper.Set_PORT(const Value: Integer);
175begin176if Value<>0then177 _PORT := Value
178end;
179180procedure TMySQLHelper.Set_HOST (const Value: string);
181begin182if Value<>''then183 _HOST := Value
184end;
185186procedure TMySQLHelper.Set_DATABASE (const Value: string);
187begin188 _DATABASE := Value
189end;
190191procedure TMySQLHelper.Set_USER_NAME (const Value: string);
192begin193 _USER_NAME := Value;
194end;
195196procedure TMySQLHelper.Set_PASSWORD (const Value: string);
197begin198 _PASSWORD := Value;
199end;
200201procedure TMySQLHelper.Set_SERVERCHARSET (const Value: string);
202begin203if Value<>''then204 _SERVERCHARSET := Value
205end;
206207end.
下载地址 MySQLHelper
以上就介绍了Delphi XE MySQL数据库操作类 MySQLHelper,包括了方面的内容,希望对Delphijrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_143131.html