zoukankan      html  css  js  c++  java
  • 对mysql.pas的封装

    引用的mysql.pas文件的版本是:04-Aug-2009.

    嗯,在上一篇中已经贴出来了.  引用的动态库libmySQL.dll 在mysql的安装目录下有.

    先把它封装成类 TMySql;

    unit MySqlClass;

    interface

    uses
    Windows, Messages, SysUtils, Classes,

    mysql;

    const
    //定义命令 M_SQL_ClientCmd()函数使用

    SHOWDATABASES
    = 101; //获得所有的数据库
    USEDB
    = 102; //使用数据库
    SHOWTABLES
    = 103; //获得所有的表
    USECHARNAME
    = 104; //使用编码

    FIELDCOUNT
    = 105; //获得结果集的字段数
    ROWCOUNT
    = 106; //获得结果集的行数

    //定义错误码 通过GetLastError()得到

    ERROR_MYSQL_NOERROR
    = 0; //无错误
    ERROR_MYSQL_INIT
    = 1; //初始化失败
    ERROR_MYSQL_CONNECT
    = 2; //连接服务器失败
    ERROR_MYSQL_GETALLBASE
    = 3; //获取databases失败
    ERROR_MYSQL_SELECTDB
    = 4; //查询table失败
    ERROR_MYSQL_GETALLTABLE
    = 5; //获取table失败
    ERROR_MYSQL_QUERY
    = 6; //执行QUERY失败
    ERROR_MYSQL_NULLPOINTER
    = 7; //空的函数指针

    type
    //回调函数
    TMySqlDataCallBack
    = procedure(nIndex: LongInt; sName: PChar; dateType: Byte;
    dwUser: DWORD);
    stdcall;

    TMySql
    = class
    private
    FLibHandle: PMYSQL;
    FmySQL_Res: PMYSQL_RES;
    FLastError: DWORD;
    FCharName:
    string;
    Fdb:
    string;
    FClientVer:
    string;
    FSerVer:
    string;
    procedure SQL_ResIni();
    function SQL_GetAllDataBase(lpBack:Pointer; UserData:LongInt):LongInt;
    function SQL_GetAllTable(db: PAnsiChar; lpBack:Pointer; UserData:LongInt):LongInt;
    procedure SetCharName(const Value: string);
    procedure Setdb(const Value: string);
    protected

    public
    function M_SQL_Init(): PAnsiChar;
    procedure M_SQL_Clear();

    function GetLastError():LongInt;

    //连接服务器 返回为服务器版本号
    function M_SQL_Connect(host, user, passwd, db:PAnsiChar; port: LongInt=3306): PAnsiChar;

    function M_SQL_ClientCmd(cmd:LongInt; names:PAnsiChar;lpBack:Pointer; UserData:LongInt):LongInt;

    //执行Query SQL语句
    function M_SQL_Query(sqlLine: PAnsiChar):LongInt;

    //Cmd 0:得到结果集 1:释放
    function M_SQL_Select_Res(sql: PAnsiChar; Cmd:Byte):Pointer;

    //获得字段
    function M_SQL_fetch_field(Res: Pointer):Pointer;
    function M_SQL_field_name(field: Pointer):PAnsiChar;

    //获得查询结果
    procedure M_SQL_data_seek(Res: Pointer; nIndex: LongInt);
    function M_SQL_fetch_row(Res: Pointer):Pointer;

    property CharName: string read FCharName write SetCharName;
    property db: string read Fdb write Setdb;
    property ClientVer: string read FClientVer;
    property SerVer: string read FSerVer;
    end;

    implementation

    function TMySql.M_SQL_Connect(host, user, passwd, db:PAnsiChar;
    port:LongInt): PAnsiChar;
    begin
    Result :
    = '';
    SQL_ResIni();

    if FLibHandle<>nil then
    begin
    mysql_close(FLibHandle);
    FLibHandle :
    = nil;
    end;

    FLibHandle :
    = mysql_init(nil);
    if FLibHandle=nil then
    begin
    FLastError :
    = ERROR_MYSQL_INIT;
    Exit;
    end;

    if mysql_real_connect(FLibHandle,host, user, passwd, db,port,nil,0) = nil then
    begin
    FLastError :
    = ERROR_MYSQL_CONNECT;
    Exit;
    end;
    Fdb :
    = db;
    Result :
    = mysql_get_server_info(FLibHandle);
    FSerVer :
    = Result;
    FCharName :
    = mysql_character_set_name(FLibHandle);
    end;

    function TMySql.SQL_GetAllDataBase(lpBack:Pointer; UserData:LongInt): LongInt;
    var
    MYSQL_ROW: PMYSQL_ROW;
    i: Integer;
    begin
    Result :
    = -1;
    FmySQL_Res :
    = mysql_list_dbs(FLibHandle, nil);
    if FmySQL_Res = nil then
    begin
    FLastError :
    = ERROR_MYSQL_GETALLBASE;
    Exit;
    end;

    if lpBack = nil then
    begin
    FLastError :
    = ERROR_MYSQL_NULLPOINTER;
    Exit;
    end;

    i :
    = 0;
    try
    repeat
    MYSQL_ROW :
    = mysql_fetch_row(FmySQL_Res);
    if MYSQL_ROW<>nil then
    TMySqlDataCallBack(lpBack)(i,MYSQL_ROW^[
    0],0,UserData);
    Inc(i);
    until MYSQL_ROW=nil;
    finally
    SQL_ResIni();
    end;
    Result :
    = 0;
    end;

    function TMySql.SQL_GetAllTable(db: PAnsiChar; lpBack:Pointer; UserData:LongInt): LongInt;
    var
    SqlResult: Integer;
    MYSQL_ROW: PMYSQL_ROW;
    i: Integer;
    begin
    Result :
    = -1;
    SQL_ResIni();

    SqlResult :
    = mysql_select_db(FLibHandle, db);
    if SqlResult <>0 then
    begin
    FLastError :
    = ERROR_MYSQL_SELECTDB;
    Exit;
    end;

    FmySQL_Res :
    = mysql_list_tables(FLibHandle, nil);
    if FmySQL_Res=nil then
    begin
    FLastError :
    = ERROR_MYSQL_GETALLTABLE;
    Exit;
    end;

    if lpBack = nil then
    begin
    FLastError :
    = ERROR_MYSQL_NULLPOINTER;
    Exit;
    end;

    i :
    = 0;
    try
    repeat
    MYSQL_ROW :
    = mysql_fetch_row(FmySQL_Res);
    if MYSQL_ROW<>nil then
    TMySqlDataCallBack(lpBack)(i,MYSQL_ROW^[
    0],1,UserData);
    Inc(i);
    until MYSQL_ROW=nil;
    finally
    SQL_ResIni();
    end;
    Result :
    = 0;
    end;

    function TMySql.M_SQL_Init: PAnsiChar;
    begin
    //加载动态库以及获得客户端版本信息
    libmysql_fast_load(
    nil);
    Result :
    = mysql_get_client_info;
    Fdb :
    = '';
    FClientVer :
    = Result;
    end;

    function TMySql.M_SQL_Query(sqlLine: PAnsiChar): LongInt;
    begin
    Result :
    = mysql_send_query(FLibHandle,sqlLine,Length(sqlLine));
    if Result <> 0 then
    FLastError :
    = ERROR_MYSQL_QUERY;
    end;

    procedure TMySql.SQL_ResIni();
    begin
    if FmySQL_Res<>nil then
    mysql_free_result(FmySQL_Res);
    FmySQL_Res :
    = nil;
    end;

    procedure TMySql.SetCharName(const Value: string);
    begin
    mysql_
    set_character_set(FLibHandle,PAnsiChar(Value));
    FCharName :
    = Value;
    end;

    function TMySql.M_SQL_ClientCmd(cmd: Integer; names:PAnsiChar; lpBack:Pointer;
    UserData:LongInt): LongInt;
    begin
    Result :
    = -1;
    case cmd of
    SHOWDATABASES: Result :
    = SQL_GetAllDataBase(lpBack,UserData); //0 成功
    USEDB:
    begin
    db :
    = names;
    Result :
    = 0;
    end;
    SHOWTABLES: Result :
    = SQL_GetAllTable(names,lpBack,UserData); //0 成功
    USECHARNAME:
    begin
    CharName :
    = names;
    Result :
    = 0;
    end;
    FIELDCOUNT:
    begin
    if FmySQL_Res <> nil then
    Result :
    = mysql_num_fields(FmySQL_Res); //字段总数
    end;
    ROWCOUNT:
    begin
    if FmySQL_Res <> nil then
    Result :
    = mysql_num_rows(FmySQL_Res); //查询的行的总数
    end;
    end;
    end;

    procedure TMySql.Setdb(const Value: string);
    begin
    mysql_select_db(FLibHandle,PAnsiChar(Value));
    Fdb :
    = Value;
    end;

    procedure TMySql.M_SQL_Clear;
    begin
    if libmysql_status=LIBMYSQL_READY then
    mysql_close(FLibHandle);
    end;

    function TMySql.GetLastError: LongInt;
    begin
    Result :
    = FLastError;
    end;

    procedure TMySql.M_SQL_data_seek(Res: Pointer; nIndex: Integer);
    begin
    mysql_data_seek(Res, nIndex);
    end;

    function TMySql.M_SQL_fetch_field(Res: Pointer): Pointer;
    begin
    Result :
    = mysql_fetch_field(Res);
    end;

    function TMySql.M_SQL_fetch_row(Res: Pointer): Pointer;
    begin
    Result :
    = mysql_fetch_row(Res);
    end;

    function TMySql.M_SQL_field_name(field: Pointer): PAnsiChar;
    begin
    Result :
    = mysql_field_name(field);
    end;

    function TMySql.M_SQL_Select_Res(sql: PAnsiChar; Cmd: Byte): Pointer;
    begin
    Result :
    = nil;
    if Cmd = 1 then
    begin
    SQL_ResIni();
    Exit;
    end;

    if mysql_real_query(FLibHandle, sql, Length(sql))<>0 then
    begin
    FLastError :
    = ERROR_MYSQL_QUERY;
    Exit;
    end;
    SQL_ResIni();
    FmySQL_Res :
    = mysql_store_result(FLibHandle);
    Result :
    = FmySQL_Res;
    end;

    end.

    然后是DLL文件: MysqlsdkMysqlsdk.dll

    Mysqlsdk
    library Mysqlsdk;
    uses
    SysUtils,
    Classes,
    MySqlClass;

    {$R *.res}

    function SQL_Init(): Pointer; stdcall;
    begin
    Result :
    = Pointer(TMySql.Create);
    if Result <> nil then
    TMySql(Result).M_SQL_Init();
    end;

    procedure SQL_Clear(sqllib: Pointer);stdcall;
    begin
    if sqllib <> nil then
    TMySql(sqllib).M_SQL_Clear();
    end;

    function SQL_LastError(sqllib: Pointer):LongInt;stdcall;
    begin
    Result :
    = -1;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).GetLastError();
    end;

    function SQL_Version(sqllib: Pointer; nType: Byte):PAnsiChar;stdcall;
    begin
    Result :
    = PAnsiChar('Version error');
    if sqllib <> nil then
    begin
    if nType = 0 then
    Result :
    = PAnsiChar(TMySql(sqllib).ClientVer)
    else if nType = 1 then
    Result :
    = PAnsiChar(TMySql(sqllib).SerVer)
    else
    Result :
    = PAnsiChar(TMySql(sqllib).CharName);
    end;

    end;

    function SQL_Connect(sqllib: Pointer;host, user, passwd, db:PAnsiChar; port: LongInt=3306): LongInt;stdcall;
    begin
    Result :
    = -1;
    if sqllib <> nil then
    begin
    if Trim(TMySql(sqllib).M_SQL_Connect(host,user,passwd,db,port)) <> '' then
    Result :
    = 0;
    end;
    end;

    function SQL_ClientCmd(sqllib: Pointer; cmd:LongInt; names:PAnsiChar;lpBack:Pointer; UserData:LongInt):LongInt;stdcall;
    begin
    Result :
    = -1;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_ClientCmd(cmd,names,lpBack,UserData);
    end;

    function SQL_Query(sqllib: Pointer; sqlLine: PAnsiChar):LongInt;stdcall;
    begin
    Result :
    = -1;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_Query(sqlLine);
    end;

    function SQL_Select_Res(sqllib: Pointer; sql: PAnsiChar; Cmd:Byte):Pointer;stdcall;
    begin
    Result :
    = nil;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_Select_Res(sql,Cmd);
    end;

    function SQL_fetch_field(sqllib: Pointer; Res: Pointer):Pointer;stdcall;
    begin
    Result :
    = nil;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_fetch_field(Res);
    end;

    function SQL_field_name(sqllib: Pointer;field: Pointer):PAnsiChar;stdcall;
    begin
    Result :
    = nil;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_field_name(field);
    end;

    function SQL_data_seek(sqllib: Pointer; Res: Pointer; nIndex: LongInt):LongInt;stdcall;
    begin
    Result :
    = -1;
    if sqllib <> nil then
    TMySql(sqllib).M_SQL_data_seek(Res,nIndex);
    end;

    function SQL_fetch_row(sqllib: Pointer; Res: Pointer):Pointer;stdcall;
    begin
    Result :
    = nil;
    if sqllib <> nil then
    Result :
    = TMySql(sqllib).M_SQL_fetch_row(Res);
    end;

    exports
    SQL_Init name
    'SQL_Init',
    SQL_Clear name
    'SQL_Clear',
    SQL_LastError name
    'SQL_LastError',
    SQL_Version name
    'SQL_Version',
    SQL_Connect name
    'SQL_Connect',
    SQL_ClientCmd name
    'SQL_ClientCmd',
    SQL_Query name
    'SQL_Query',
    SQL_Select_Res name
    'SQL_Select_Res',
    SQL_fetch_field name
    'SQL_fetch_field',
    SQL_field_name name
    'SQL_field_name',
    SQL_data_seek name
    'SQL_data_seek',
    SQL_fetch_row name
    'SQL_fetch_row';
    begin
    end.

    这样封装之后,不管是delphi还是c++都可以访问了.

    delphi下的导出pas;

    代码
    unit Mysqlsdk;

    interface
    uses
    Windows;
    const
    SDK
    = 'Mysqlsdk.dll';
    //命令
    SHOWDATABASES
    = 101;
    USEDB
    = 102;
    SHOWTABLES
    = 103;
    USECHARNAME
    = 104;

    FIELDCOUNT
    = 105;
    ROWCOUNT
    = 106;
    //错误码
    ERROR_MYSQL_NOERROR
    = 0; //无错误
    ERROR_MYSQL_INIT
    = 1; //初始化失败
    ERROR_MYSQL_CONNECT
    = 2; //连接服务器失败
    ERROR_MYSQL_GETALLBASE
    = 3; //获取databases失败
    ERROR_MYSQL_SELECTDB
    = 4; //查询table失败
    ERROR_MYSQL_GETALLTABLE
    = 5; //获取table失败
    ERROR_MYSQL_QUERY
    = 6; //执行QUERY失败
    ERROR_MYSQL_NULLPOINTER
    = 7; //空地址

    type
    PMYSQL_ROW
    = ^TMYSQL_ROW; // return data as array of strings
    TMYSQL_ROW
    = array[0..MaxInt div SizeOf(PAnsiChar) - 1] of PAnsiChar;

    {-------------------------------------------
    使用的回调函数的定义
    nIndex 序号
    DataType 0: 数据库数据 1: 表数据;
    UserData: 自定义数据;

    TMySqlDataCallBack = procedure(nIndex:Integer; Data1: PAnsiChar; DataType:Byte;
    UserData: Cardinal); stdcall;
    *********************************************************************************************
    --------------------------------------------
    }

    function SQL_Init(): Pointer; stdcall; external SDK;

    procedure SQL_Clear(sqllib: Pointer);stdcall; external SDK;


    {-----------------------------------------------------
    |SQL_LastError()函数 |
    |错误码获取 相关说明在常量里有说明 |
    ------------------------------------------------------
    }
    function SQL_LastError(sqllib: Pointer):LongInt;stdcall; external SDK;


    {-----------------------------------------------------
    |SQL_Version函数 |
    |nType 0:客户端版本 1:服务器版本 2:编码类型 |
    ------------------------------------------------------
    }
    function SQL_Version(sqllib: Pointer; nType: Byte):PAnsiChar;stdcall; external SDK;


    {------------------------------------------------------
    |db 可设置为nil,也可以设置为具体的数据库 |
    -------------------------------------------------------
    }
    function SQL_Connect(sqllib: Pointer;host, user, passwd, db:PAnsiChar;
    port: LongInt
    =3306): LongInt;stdcall; external SDK;


    {---------------------------------------------------------------------
    |SQL_ClientCmd()函数 |
    |cmd | 作用 | names | lpBack | UserData |
    |SHOWDATABASES | 显示所有数据库| 无用 | 回调 | 自定义数据 |
    |USEDB | 使用数据库 | 数据库名 | 无用 | 无用 |
    |SHOWTABLES | 显示所有表 | 数据库名 | 回调 | 自定义数据 |
    |USECHARNAME | 设置编码 | 编码名称 | 无用 | 无用 |
    | | |
    ----------------------------------------------------------------------
    }
    function SQL_ClientCmd(sqllib: Pointer; cmd:LongInt; names:PAnsiChar;
    lpBack:Pointer; UserData:LongInt):LongInt;
    stdcall; external SDK;

    {--------------------------------------------------------------------------------------
    |SQL_Query()函数 执行sql语句 |
    |sqlLine: sql语句; |
    |比如:-----表的创建: create table my(id int(4),memo varchar(200),tt date); |
    | -----增: insert into my values(1,"我是中国人,我热爱自己的祖国啊","2009-09-21"); |
    | -----删: delete from my where id=2; |
    | -----改: update my set memo="看,过来一个美女" where id=1; |
    |-----数据库的创建: create database ok; |
    |-----数据库的删除: drop database ok; |
    | |
    |注意: 插入中文的时候 注意设置编码为gbk 或者 gb2312 |
    --------------------------------------------------------------------------------------
    }
    function SQL_Query(sqllib: Pointer; sqlLine: PAnsiChar):LongInt;stdcall; external SDK;

    {**************************************************************************************}
    function SQL_Select_Res(sqllib: Pointer; sql: PAnsiChar; Cmd:Byte):Pointer;stdcall;external SDK;

    function SQL_fetch_field(sqllib: Pointer; Res: Pointer):Pointer;stdcall;external SDK;

    function SQL_field_name(sqllib: Pointer;field: Pointer):PAnsiChar;stdcall;external SDK;

    function SQL_data_seek(sqllib: Pointer; Res: Pointer; nIndex: LongInt):LongInt;stdcall;external SDK;

    function SQL_fetch_row(sqllib: Pointer; Res: Pointer):Pointer;stdcall;external SDK;

    {***************************************************************************************}
    implementation

    end.

    delphi demo:

    代码
    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Mysqlsdk, ComCtrls, Buttons;

    type
    TForm1
    = class(TForm)
    btn2: TButton;
    ListView1: TListView;
    edt1: TEdit;
    edt2: TEdit;
    edt3: TEdit;
    edt4: TEdit;
    Lbl1: TLabel;
    Lbl2: TLabel;
    Lbl3: TLabel;
    Lbl4: TLabel;
    btn4: TButton;
    Lbl5: TLabel;
    Lbl6: TLabel;
    Lbl7: TLabel;
    Lbl8: TLabel;
    Lbl9: TLabel;
    Lbl10: TLabel;
    lst1: TListBox;
    lst2: TListBox;
    Lbl11: TLabel;
    Lbl12: TLabel;
    edt5: TEdit;
    btn1: TButton;
    cbb1: TComboBox;
    Lbl13: TLabel;
    edt6: TEdit;
    btn3: TButton;
    btn6: TSpeedButton;
    btn7: TSpeedButton;
    btn8: TSpeedButton;
    btn9: TSpeedButton;
    btn10: TSpeedButton;
    Lbl14: TLabel;
    btn11: TButton;
    btn12: TButton;
    Lbl15: TLabel;
    Lbl16: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure lst1Click(Sender: TObject);
    procedure lst2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);
    procedure btn11Click(Sender: TObject);
    procedure cbb1Change(Sender: TObject);
    procedure btn12Click(Sender: TObject);
    private
    { Private declarations }
    Fmysql: Pointer;
    FRes: Pointer;
    public
    function GetResult(lv: TListView; sqlLine: string): Integer;
    { Public declarations }
    end;


    var
    Form1: TForm1;
    procedure bbb(nIndex:Integer; Data1: PAnsiChar; DataType:Byte;
    UserData: Cardinal);
    stdcall;
    implementation

    {$R *.dfm}
    procedure bbb(nIndex:Integer; Data1: PAnsiChar; DataType:Byte;
    UserData: Cardinal);
    begin
    if DataType = 0 then
    begin
    TForm1(UserData).lst1.Items.Add(Data1);
    end else
    begin
    TForm1(UserData).lst2.Items.Add(Data1);
    end;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    Fmysql :
    = SQL_Init();
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    SQL_Clear(Fmysql);
    end;

    procedure TForm1.btn4Click(Sender: TObject);
    begin
    if SQL_Connect(Fmysql,PAnsiChar(edt1.Text),PAnsiChar(edt2.Text),PAnsiChar(edt3.Text),
    PAnsiChar(edt4.Text))
    <> -1 then
    begin
    Lbl8.Caption :
    = SQL_Version(Fmysql,0);
    Lbl9.Caption :
    = SQL_Version(Fmysql,1);
    Lbl10.Caption :
    = SQL_Version(Fmysql,2);
    end else
    Lbl16.Caption :
    = IntToStr(SQL_LastError(Fmysql));
    end;

    procedure TForm1.btn2Click(Sender: TObject);
    begin
    lst1.Clear;
    SQL_ClientCmd(Fmysql,SHOWDATABASES,
    nil,@bbb,DWORD(Self));
    end;

    procedure TForm1.lst1Click(Sender: TObject);
    begin
    lst2.Clear();
    SQL_ClientCmd(Fmysql,SHOWTABLES,PAnsiChar(lst1.Items[lst1.ItemIndex]),@bbb,DWORD(Self));
    end;

    procedure TForm1.lst2Click(Sender: TObject);
    var
    sql:
    string;
    nCount: Integer;
    begin
    sql :
    = 'select * from ' + lst2.Items[lst2.ItemIndex];
    nCount :
    = GetResult(ListView1,sql);
    Lbl12.Caption :
    = IntToStr(nCount);
    end;

    procedure TForm1.btn1Click(Sender: TObject);
    begin
    SQL_Query(Fmysql,PAnsiChar(edt5.text));
    end;

    procedure TForm1.btn3Click(Sender: TObject);
    begin
    SQL_ClientCmd(Fmysql,USEDB,PAnsiChar(edt6.Text),
    nil,0);
    end;

    procedure TForm1.btn6Click(Sender: TObject);
    begin
    edt5.Text :
    = edt5.Text + TSpeedButton(Sender).Caption;
    end;

    procedure TForm1.btn11Click(Sender: TObject);
    begin
    edt5.Text :
    = '';
    end;

    procedure TForm1.cbb1Change(Sender: TObject);
    begin
    SQL_ClientCmd(Fmysql,USECHARNAME,PAnsiChar(cbb1.Text),
    nil,0);
    Lbl10.Caption :
    = SQL_Version(Fmysql,2);
    end;

    procedure TForm1.btn12Click(Sender: TObject);
    var
    sql:
    string;
    nCount: Integer;
    begin
    sql :
    = edt5.Text;
    nCount :
    = GetResult(ListView1,sql);
    Lbl12.Caption :
    = IntToStr(nCount);
    end;

    function TForm1.GetResult(lv: TListView; sqlLine: string): Integer;
    var
    mySQL_Field: Pointer;
    i, row, Field, j: Integer;
    MYSQL_ROW: PMYSQL_ROW;
    begin
    lv.Clear;
    lv.Columns.Clear;
    Result :
    = -1;
    //开始查找
    FRes :
    = SQL_Select_Res(Fmysql,PAnsiChar(sqlLine),0);
    if FRes <> nil then
    begin
    //字段数
    Field :
    = SQL_ClientCmd(Fmysql,FIELDCOUNT,nil,nil,0);
    //结果行数
    row :
    = SQL_ClientCmd(Fmysql,ROWCOUNT,nil,nil,0);
    Result :
    = row;

    //得到字段名称
    for i := 0 to Field-1 do
    begin
    mySQL_Field :
    = SQL_fetch_field(Fmysql,FRes);
    if mySQL_Field<>nil then
    begin
    lv.Columns.Add.Caption :
    = SQL_field_name(Fmysql,mySQL_Field);
    end;
    end;

    //取得结果集
    i :
    = 0;
    while i< row do
    begin
    SQL_data_seek(Fmysql,FRes, i);
    MYSQL_ROW :
    = PMYSQL_ROW(SQL_fetch_row(Fmysql,FRes));
    if MYSQL_ROW <> nil then
    begin
    with lv.Items.Add do
    begin
    Caption :
    = MYSQL_ROW^[0];
    for j := 1 to Field-1 do
    begin
    SubItems.Add(MYSQL_ROW^[j]);
    end;
    end;
    end;
    Inc(i);
    end;
    //释放结果集
    SQL_Select_Res(Fmysql,
    '',1);
    end;
    end;

    end.

    窗体文件:

    代码
    object Form1: TForm1
    Left
    = 357
    Top
    = 159
    Width
    = 740
    Height
    = 616
    Caption
    = 'Form1'
    Color
    = clBtnFace
    Font.Charset
    = GB2312_CHARSET
    Font.Color
    = clWindowText
    Font.Height
    = -13
    Font.Name
    = '宋体'
    Font.Style
    = []
    OldCreateOrder
    = False
    OnCreate
    = FormCreate
    OnDestroy
    = FormDestroy
    PixelsPerInch
    = 96
    TextHeight
    = 13
    object Lbl1: TLabel
    Left
    = 24
    Top
    = 19
    Width
    = 39
    Height
    = 13
    Caption
    = '服务器'
    end
    object Lbl2: TLabel
    Left
    = 24
    Top
    = 61
    Width
    = 39
    Height
    = 13
    Caption
    = '用户名'
    end
    object Lbl3: TLabel
    Left
    = 23
    Top
    = 103
    Width
    = 40
    Height
    = 13
    Caption
    = '密 码'
    end
    object Lbl4: TLabel
    Left
    = 23
    Top
    = 140
    Width
    = 39
    Height
    = 13
    Caption
    = '数据库'
    end
    object Lbl5: TLabel
    Left
    = 240
    Top
    = 16
    Width
    = 65
    Height
    = 13
    Caption
    = '服务器版本'
    end
    object Lbl6: TLabel
    Left
    = 240
    Top
    = 48
    Width
    = 65
    Height
    = 13
    Caption
    = '客户端版本'
    end
    object Lbl7: TLabel
    Left
    = 240
    Top
    = 77
    Width
    = 52
    Height
    = 13
    Caption
    = '使用语言'
    end
    object Lbl8: TLabel
    Left
    = 320
    Top
    = 16
    Width
    = 7
    Height
    = 13
    end
    object Lbl9: TLabel
    Left
    = 320
    Top
    = 48
    Width
    = 7
    Height
    = 13
    end
    object Lbl10: TLabel
    Left
    = 320
    Top
    = 80
    Width
    = 7
    Height
    = 13
    end
    object Lbl11: TLabel
    Left
    = 344
    Top
    = 144
    Width
    = 52
    Height
    = 13
    Caption
    = '查询结果'
    end
    object Lbl12: TLabel
    Left
    = 400
    Top
    = 144
    Width
    = 7
    Height
    = 13
    end
    object Lbl13: TLabel
    Left
    = 24
    Top
    = 240
    Width
    = 52
    Height
    = 13
    Caption
    = '设置编码'
    end
    object btn6: TSpeedButton
    Left
    = 134
    Top
    = 174
    Width
    = 49
    Height
    = 22
    Caption
    = 'create'
    Flat
    = True
    OnClick
    = btn6Click
    end
    object btn7: TSpeedButton
    Left
    = 190
    Top
    = 174
    Width
    = 57
    Height
    = 22
    Caption
    = 'insert'
    Flat
    = True
    OnClick
    = btn6Click
    end
    object btn8: TSpeedButton
    Left
    = 254
    Top
    = 174
    Width
    = 65
    Height
    = 22
    Caption
    = 'update'
    Flat
    = True
    OnClick
    = btn6Click
    end
    object btn9: TSpeedButton
    Left
    = 326
    Top
    = 174
    Width
    = 65
    Height
    = 22
    Caption
    = 'drop'
    Flat
    = True
    OnClick
    = btn6Click
    end
    object btn10: TSpeedButton
    Left
    = 398
    Top
    = 174
    Width
    = 49
    Height
    = 22
    Caption
    = 'rename'
    Flat
    = True
    OnClick
    = btn6Click
    end
    object Lbl14: TLabel
    Left
    = 32
    Top
    = 179
    Width
    = 91
    Height
    = 13
    Caption
    = '点击使用关键字'
    end
    object Lbl15: TLabel
    Left
    = 240
    Top
    = 104
    Width
    = 39
    Height
    = 13
    Caption
    = '错误码'
    end
    object Lbl16: TLabel
    Left
    = 320
    Top
    = 104
    Width
    = 7
    Height
    = 13
    end
    object btn2: TButton
    Left
    = 392
    Top
    = 72
    Width
    = 75
    Height
    = 25
    Caption
    = '查询数据库'
    TabOrder
    = 0
    OnClick
    = btn2Click
    end
    object ListView1: TListView
    Left
    = 8
    Top
    = 316
    Width
    = 465
    Height
    = 249
    Columns
    = <>
    GridLines
    = True
    TabOrder
    = 1
    ViewStyle
    = vsReport
    end
    object edt1: TEdit
    Left
    = 80
    Top
    = 16
    Width
    = 121
    Height
    = 21
    TabOrder
    = 2
    Text
    = 'localhost'
    end
    object edt2: TEdit
    Left
    = 80
    Top
    = 56
    Width
    = 121
    Height
    = 21
    TabOrder
    = 3
    Text
    = 'root'
    end
    object edt3: TEdit
    Left
    = 80
    Top
    = 96
    Width
    = 121
    Height
    = 21
    PasswordChar
    = '*'
    TabOrder
    = 4
    Text
    = 'root'
    end
    object edt4: TEdit
    Left
    = 80
    Top
    = 136
    Width
    = 121
    Height
    = 21
    TabOrder
    = 5
    end
    object btn4: TButton
    Left
    = 232
    Top
    = 136
    Width
    = 75
    Height
    = 25
    Caption
    = '连接'
    TabOrder
    = 6
    OnClick
    = btn4Click
    end
    object lst1: TListBox
    Left
    = 488
    Top
    = 8
    Width
    = 233
    Height
    = 161
    ItemHeight
    = 13
    TabOrder
    = 7
    OnClick
    = lst1Click
    end
    object lst2: TListBox
    Left
    = 488
    Top
    = 184
    Width
    = 233
    Height
    = 393
    ItemHeight
    = 13
    TabOrder
    = 8
    OnClick
    = lst2Click
    end
    object edt5: TEdit
    Left
    = 24
    Top
    = 204
    Width
    = 425
    Height
    = 21
    TabOrder
    = 9
    end
    object btn1: TButton
    Left
    = 256
    Top
    = 233
    Width
    = 97
    Height
    = 25
    Caption
    = '执行SQL语句'
    TabOrder
    = 10
    OnClick
    = btn1Click
    end
    object cbb1: TComboBox
    Left
    = 80
    Top
    = 236
    Width
    = 121
    Height
    = 21
    ItemHeight
    = 13
    ItemIndex
    = 0
    TabOrder
    = 11
    Text
    = 'latin1'
    OnChange
    = cbb1Change
    Items.Strings
    = (
    'latin1'
    'latin2'
    'utf8'
    'gb2312'
    'gbk')
    end
    object edt6: TEdit
    Left
    = 24
    Top
    = 284
    Width
    = 81
    Height
    = 21
    TabOrder
    = 12
    end
    object btn3: TButton
    Left
    = 120
    Top
    = 284
    Width
    = 75
    Height
    = 25
    Caption
    = '使用数据库'
    TabOrder
    = 13
    OnClick
    = btn3Click
    end
    object btn11: TButton
    Left
    = 368
    Top
    = 232
    Width
    = 75
    Height
    = 25
    Caption
    = '清空语句'
    TabOrder
    = 14
    OnClick
    = btn11Click
    end
    object btn12: TButton
    Left
    = 352
    Top
    = 280
    Width
    = 97
    Height
    = 25
    Caption
    = '复杂搜索'
    TabOrder
    = 15
    OnClick
    = btn12Click
    end
    end
  • 相关阅读:
    L1-031 到底是不是太胖了
    L1-030 一帮一
    PyCharm--git配置
    websocket--python
    UDP--python
    TCP--python
    pytest--metadata
    pytest--xdist
    pytest--夹具
    pytest--变量
  • 原文地址:https://www.cnblogs.com/doorsky/p/1639980.html
Copyright © 2011-2022 走看看