[fpc-pascal] MySQL50 unit in freepascal
Carsten Bager
carsten at beas.dk
Tue Apr 22 13:08:32 CEST 2008
> Does anyone have a simple (very simple) and basic way of connecting to a
> mysql database, reading a record and writing a record into a table? That's
> all I need to get started; or if there is some documentation somewhere on
> how to use the unit id be grateful.
Here is an example I found somewhere. It works, I have tested it on my
MySql database server (Novel Netware).
Carsten
---------------------------------------------------------
program qtest;
uses
mysql50;
Const
DataBase : Pchar = 'time_sedler';
Query : Pchar = 'Select * from maanedstabel';
var
count,num : longint;
code : integer;
sock : PMYSQL;
qmysql : st_mysql;
qbuf : string [160];
rowbuf : MYSQL_ROW;
dummy : string;
recbuf : PMYSQL_RES;
begin
if paramcount=1 then
begin
Dummy:=Paramstr(1)+#0;
DataBase:=@Dummy[1];
end;
Write ('Connecting to MySQL...');
mysql_init(PMySQL(@qmysql));
sock :=
mysql_real_connect(PMysql(@qmysql),'80.80.80.80','root','password',nil,33
06,nil,0);
if sock=Nil then
begin
Writeln (stderr,'Couldn''t connect to MySQL.');
Writeln (stderr,mysql_error(@qmysql));
halt(1);
end;
Writeln ('Done.');
Writeln ('Connection data:');
{$ifdef Unix}
writeln ('Mysql_port : ',mysql_port);
writeln ('Mysql_unix_port : ',mysql_unix_port);
{$endif}
writeln ('Host info : ',mysql_get_host_info(sock));
writeln ('Server info : ',mysql_stat(sock));
writeln ('Client info : ',mysql_get_client_info);
Writeln ('Selecting Database ',DataBase,'...');
if mysql_select_db(sock,DataBase) < 0 then
begin
Writeln (stderr,'Couldn''t select database ',Database);
Writeln (stderr,mysql_error(sock));
halt (1);
end;
writeln ('Executing query : ',Query,'...');
if (mysql_query(sock,Query) < 0) then
begin
Writeln (stderr,'Query failed ');
writeln (stderr,mysql_error(sock));
Halt(1);
end;
recbuf := mysql_store_result(sock);
if RecBuf=Nil then
begin
Writeln ('Query returned nil result.');
mysql_close(sock);
halt (1);
end;
Writeln ('Number of records returned : ',mysql_num_rows (recbuf));
Writeln ('Number of fields per record : ',mysql_num_fields(recbuf));
rowbuf := mysql_fetch_row(recbuf);
while (rowbuf <>nil) do
begin
Write ('(Id: ', rowbuf[0]);
Write (', Name: ', rowbuf[1]);
Writeln(', Email : ', rowbuf[2],')');
rowbuf := mysql_fetch_row(recbuf);
end;
Writeln ('Freeing memory occupied by result set...');
mysql_free_result (recbuf);
Writeln ('Closing connection with MySQL.');
mysql_close(sock);
halt(0);
end.
More information about the fpc-pascal
mailing list