📄 mysqlclass.pas
字号:
unit mysqlclass;
interface
uses SysUtils,mysql;
type
TMySQLClass = class(TObject)
private
FHostname: string;
FUsername: string;
FPassword: string;
FDatabase: string;
FPort: Integer;
FMySQL: PMYSQL; //连接线路
FResult: PMYSQL_RES; //Query结果集
FRow: PMYSQL_ROW; //Query行结果
FFields: PMYSQL_FIELDS; //Query字段集
FActive: Boolean;
FRows: my_ulonglong;
FCols: Integer;
FRowIndex: my_ulonglong;
FEof: Boolean;
FBof: Boolean;
procedure FInit;
function FConnected:Boolean;
procedure FreeResult;
procedure FetchRow;
function IndexOfName(FieldName:string):Integer;
public
constructor Create(var Hostname,Username,Password,Database:String ; Port:Integer);
destructor Destroy; override;
function Query(StatementSQL:string):Integer;
function IsConnected:Boolean;
function IsEof :Boolean;
function IsBof :Boolean;
function RowCount :my_ulonglong;
function RowIndex :my_ulonglong;
function ColCount :longword;
function FieldByName(FieldName:string) :string;
function FieldByIndex(FieldIndex:Integer):string;
procedure First;
procedure Last;
procedure Next;
procedure Prior;
end;
{==================================================================================}
implementation
{----------------------------------------------------------------------------------}
constructor TMySQLClass.Create(var Hostname,Username,Password,Database:string;Port:Integer);
begin
FHostname :=Hostname;
FUsername :=Username;
FPassword :=Password;
FDatabase :=Database;
FPort :=Port;
FInit;
end;
{----------------------------------------------------------------------------------}
destructor TMySQLClass.Destroy;
begin
FreeResult;
if FConnected then mysql_close(FMySQL);
inherited;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.FInit;
begin
FActive :=False;
FMySQL :=mysql_init(nil);
FResult :=nil;
FRow :=nil;
FFields :=nil;
FRows :=0;
FCols :=0;
FRowIndex :=0;
FEof :=True;
FBof :=True;
if
(FMySQL<>nil)
and
(mysql_real_connect(FMySQL,pAnsiChar(FHostname),pAnsiChar(FUsername),pAnsiChar(FPassword),pAnsiChar(FDatabase),FPort,nil,0)<>nil)
then FActive:=True;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.FreeResult;
begin
if FResult<>nil then
begin
mysql_free_result(FResult);
FResult :=nil;
FRow :=nil;
FFields :=nil;
FRows :=0;
FCols :=0;
FRowIndex :=0;
FEof :=True;
FBof :=True;
end;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.FConnected:Boolean;
begin
if FActive then
begin
if mysql_ping(FMySQL)<>0 then
begin
if mysql_ping(FMySQL)<>0 then FActive:=False
else FActive:=True;
end;
end;
Result:=FActive;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.IsConnected:Boolean;
begin
Result:=FConnected;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.Query(StatementSQL:string):Integer;
begin
Result:=-1;
if FConnected then
begin
FreeResult;
if mysql_real_query(FMySQL,pChar(StatementSQL),Length(StatementSQL)) =0 then
begin
FResult := mysql_store_result(FMySQL);
if FResult<>nil then
begin
FRows :=mysql_num_rows(FResult);
FCols :=mysql_num_fields(FResult);
FFields :=mysql_fetch_fields(FResult);
First;
Result :=FRows;
end
else
begin
if (mysql_errno(FMySQL)=0) and (mysql_field_count(FMySQL)=0)
then Result:=mysql_affected_rows(FMySQL);
end;
end;
end;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.RowCount :my_ulonglong;
begin
Result:=FRows;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.RowIndex :my_ulonglong;
begin
Result:=FRowIndex;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.ColCount :longword;
begin
Result:=FCols;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.IsEof :Boolean;
begin
Result:=FEof;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.IsBof :Boolean;
begin
Result:=FBof;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.First;
begin
if FResult<>nil then
begin
FRowIndex:=0;
FEof:=False;
FBof:=False;
FetchRow;
end;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.Last;
begin
if FResult<>nil then
begin
FRowIndex:=FRows-1;
FEof:=False;
FBof:=False;
FetchRow;
end;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.Next;
begin
if FResult<>nil then
begin
if (FRowIndex+1)<FRows then
begin
Inc(FRowIndex);
FetchRow;
end
else
begin
FEof:=True;
end;
end;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.Prior;
begin
if FResult<>nil then
begin
if FRowIndex>0 then
begin
Dec(FRowIndex);
FetchRow;
end
else
begin
FBof:=True;
end;
end;
end;
{----------------------------------------------------------------------------------}
procedure TMySQLClass.FetchRow;
begin
mysql_data_seek(FResult,FRowIndex);
FRow:=mysql_fetch_row(FResult);
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.IndexOfName(FieldName:string):Integer;
var IndexNo:Integer;
begin
Result:=-1;
if FFields<>nil then
begin
for IndexNo:=0 to (FCols-1) do
if AnsiStrIComp(pAnsiChar(Trim(FieldName)),FFields[IndexNo].name)=0 then
begin
Result:=IndexNo;
Break;
end;
end;
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.FieldByName(FieldName:string):string;
begin
Result:=FieldByIndex(IndexOfName(FieldName));
end;
{----------------------------------------------------------------------------------}
function TMySQLClass.FieldByIndex(FieldIndex:Integer):string;
begin Result:='';
if (FRow<>nil) and (FieldIndex>=0) and (FieldIndex<FCols) then
begin
Result:=FRow[FieldIndex];
end;
end;
{----------------------------------------------------------------------------------}
//对注册表进行操作
{==================================================================================}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -