Link to home
Start Free TrialLog in
Avatar of netscape
netscape

asked on

MySQL and Delphi

Can someone provide me with an example on how to access a MySQL database and give me a working header for version 3.22.X

Please, I need this really urgent!

Thank you very much

Green
Avatar of jconde
jconde

Hi, I have seen many components that help you doing that.....

But, that wouldnt be answering your question :)

This unit is called libmysql.pas and I modified it my self to work for 3.22.X  I have seen a new version out there (an the mysql site http://www.tcx.se) but this one works fine.

unit libmysql;
interface
uses
  sysutils;

const
 mysql_errmsg_size=200;

 field_type_decimal=0;
 field_type_tiny=1;
 field_type_short=2;
 field_type_long=3;
 field_type_float=4;
 field_type_double=5;
 field_type_null=6;
 field_type_timestamp=7;
 field_type_longlong=8;
 field_type_int24=9;
 field_type_date=10;
 field_type_time=11;
 field_type_datetime=12;
 field_type_enum=247;
 field_type_set=248;
 field_type_tiny_blob=249;
 field_type_medium_blob=250;
 field_type_long_blob=251;
 field_type_blob=252;
 field_type_var_string=253;
 field_type_string=254;

type
 enum_field_types=byte;
 my_bool= shortint;
 gptr= pchar;
 socket= word;

 mysql_status=(
mysql_status_ready,mysql_status_get_result,mysql_status_use_result);

 pused_mem=^used_mem;
 err_proc=procedure;

 used_mem=record
            next:pused_mem;
            left,size:word
           end;

 pmem_root=^mem_root;
 mem_root=record
           free,used:pused_mem;
           min_malloc,block_size:word;
           error_handler:err_proc;
          end;

  net=record
       fd: socket;
       fcntl: integer;
       buff,buff_end,write_pos: pchar;
       last_error:array[01..mysql_errmsg_size] of char;
       last_errno,max_packet,timeout,pkt_vnr: integer;
       error,return_errno:my_bool;
      end;

pmysql_field=^mysql_field;
mysql_field=record
             name,table,def: pchar;
             _type: enum_field_types;
             length,max_length,flags,decimals:integer;
            end;

pmysql_rows=^mysql_rows;
mysql_rows=record
            next: pmysql_rows;
            data:pointer ;
           end;

mysql_row=array[00..$ffff div sizeof(pchar)] of pchar;
pmysql_row=^mysql_row;

pmysql_data=pointer;

pmysql_res=^mysql_res;
mysql_res= record
  row_count: longint;
  field_count, current_field:word;
  fields: pmysql_field;
  data: pmysql_data;
  data_cursor:pmysql_rows;
  field_alloc:pmem_root;
  row:pmysql_row;
  current_row:pmysql_row;
  lengths:^word;
  handle:word;
  eof:my_bool;
 end;

pmysql=^mysql;
mysql= record
        _net: net;
        host,user,passwd,
        unix_socket,
        server_version,
        host_info,
        info,db: pchar;
        port,client_flag,server_capabilities,
        protocol_version,field_count: integer;
        thread_id,
        affected_rows,
        insert_id,
        extra_info:longint;
        status: mysql_status;
        fields: pmysql_field;
        field_alloc: mem_root;
        free_me,reconnect:my_bool;
       end;

const thelib='libmysql.dll';
Function mysql_num_rows (res : pmysql_res) : Cardinal;
Function mysql_affected_rows (res:pmysql):longint;
Function mysql_num_fields(res : pmysql_res) : Cardinal;
function mysql_drop_db(_mysql:pmysql; const db:Pchar) : Integer; Stdcall;external thelib;
//function mysql_connect ( _mysql: pmysql; const host,user,passwd,port:pchar):pmysql;
function mysql_connect( _mysql: pmysql; const host,user,passwd:pchar):pmysql;stdcall;external thelib;
function mysql_init (_mysql: pmysql):pmysql;stdcall;external thelib;
function mysql_real_connect(mysql : pmysql;host,user, passwd, db:pchar; port:cardinal; unix_socket:pchar; clientflag:integer): PMYSQL; stdcall; external thelib;

procedure mysql_close( _mysql: pmysql);stdcall;external thelib;
function mysql_stat(_mysql:pmysql):pchar;stdcall;external thelib;
Function mysql_shutdown(mysql : PMYSQL) : longint; stdcall;external thelib;
Function mysql_get_host_info(mysql : PMYSQL) : pchar;stdcall; external thelib;
Function mysql_get_server_info(mysql : PMYSQL) : pchar;stdcall; external thelib;
Function mysql_get_client_info : pchar;stdcall; external thelib;
Function mysql_get_proto_info(mysql : PMYSQL) : Cardinal;stdcall; external thelib;
function mysql_create_db(_mysql : pmysql; db : pchar) : longint;stdcall;external thelib;
Function mysql_error(mysql : PMYSQL) : Pchar;
function mysql_list_dbs(_mysql:pmysql;wild:
pchar):pmysql_res;stdcall;external thelib;
function mysql_list_tables(_mysql: pmysql; const wild:
pchar):pmysql_res;stdcall;external thelib;
function mysql_list_fields(_mysql: pmysql;const table,wild:
pchar):pmysql_res;stdcall;external thelib;
function mysql_list_processes(_mysql: pmysql):pmysql_res;stdcall;external thelib;
function mysql_select_db(_mysql:pmysql;const db:
pchar):integer;stdcall;external thelib;
function mysql_query(_mysql:pmysql;const query:
pchar):integer;stdcall;external thelib;
function mysql_store_result(_mysql:pmysql):pmysql_res;stdcall;external thelib;
function mysql_use_result(_mysql:pmysql):pmysql_res;stdcall;external
thelib;
procedure mysql_free_result(result:pmysql_res);stdcall;external thelib;
Function mysql_refresh(mysql : PMYSQL; refresh_options : cardinal) : longint;stdcall;external thelib;
function mysql_fetch_field(handle:
pmysql_res):pmysql_field;stdcall;external thelib;
function mysql_fetch_row(res:pmysql_res):pmysql_row;stdcall;external
thelib;
function mysql_kill(_mysql:pmysql;pid:integer):Integer;stdcall;external thelib;

implementation

Function mysql_affected_rows (res:pmysql):longint;
begin
  mysql_affected_rows := res^.affected_rows;
end;

Function mysql_num_rows (res : pmysql_res) : Cardinal;
begin
 mysql_num_rows:=res^.row_count
end;

Function mysql_num_fields(res : PMYSQL_RES) : Cardinal;
begin
  mysql_num_fields:=res^.field_count;
end;

Function mysql_error(mysql : pmysql) : PChar;
begin
 mysql_error := @mysql^._net.last_error;
end;
{
function mysql_connect ( _mysql: pmysql; const host,user,passwd,port:pchar):pmysql;
var
  puerto : Cardinal;
begin
  puerto := strtoint (strpas(port));
  mysql_connect := mysql_real_connect (_mysql, host, user, passwd, puerto, '0', 0);
end;
 }
end.


A sample query follows:

procedure TForm1.Button1Click(Sender: TObject);
var
  sock2,sock : pmysql;
begin
              sock2 := mysql_init(nil);
              sock := mysql_real_connect(sock2,'Host','User','Password','Database',port,'0',0);
              if sock = nil then
                messagedlg('Connection to MySQL server failed!', mtError, [mbok],0)
              else
                  if mysql_Query(sock, 'CREATE TABLE Test (aux varchar(50))' < 0 then
                      messagedlg(strpas(mysql_error(sock)), mterror, [mbok],0);
               MySQL_Close(Sock);
end;

Avatar of netscape

ASKER

Jconde,

I tested your code and it worked fine, thank you very much.

you may post your answer.

BTW, what do you know about those components you mention....to they make handling mysql as easy as using all the BDE components?

thank you very much,

Green

ASKER CERTIFIED SOLUTION
Avatar of jconde
jconde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adjusted points to 1800