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
Please, I need this really urgent!
Thank you very much
Green
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 1800
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_s
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
last_errno,max_packet,time
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,de
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_ca
protocol_version,field_cou
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:pmysq
//function mysql_connect ( _mysql: pmysql; const host,user,passwd,port:pcha
function mysql_connect( _mysql: pmysql; const host,user,passwd:pchar):pm
function mysql_init (_mysql: pmysql):pmysql;stdcall;ext
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):
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(mysq
Function mysql_get_client_info : pchar;stdcall; external thelib;
Function mysql_get_proto_info(mysql
function mysql_create_db(_mysql : pmysql; db : pchar) : longint;stdcall;external thelib;
Function mysql_error(mysql : PMYSQL) : Pchar;
function mysql_list_dbs(_mysql:pmys
pchar):pmysql_res;stdcall;
function mysql_list_tables(_mysql: pmysql; const wild:
pchar):pmysql_res;stdcall;
function mysql_list_fields(_mysql: pmysql;const table,wild:
pchar):pmysql_res;stdcall;
function mysql_list_processes(_mysq
function mysql_select_db(_mysql:pmy
pchar):integer;stdcall;ext
function mysql_query(_mysql:pmysql;
pchar):integer;stdcall;ext
function mysql_store_result(_mysql:
function mysql_use_result(_mysql:pm
thelib;
procedure mysql_free_result(result:p
Function mysql_refresh(mysql : PMYSQL; refresh_options : cardinal) : longint;stdcall;external thelib;
function mysql_fetch_field(handle:
pmysql_res):pmysql_field;s
function mysql_fetch_row(res:pmysql
thelib;
function mysql_kill(_mysql:pmysql;p
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_c
end;
Function mysql_num_fields(res : PMYSQL_RES) : Cardinal;
begin
mysql_num_fields:=res^.fie
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:pcha
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
var
sock2,sock : pmysql;
begin
sock2 := mysql_init(nil);
sock := mysql_real_connect(sock2,'
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_er
MySQL_Close(Sock);
end;