В папке \examples есть файл testdb4.pp. Эта программа обеспечивает доступ к базам данных MySQL версии 4. Но ей необходима библиотека libmysql.dll. Пришлось скачать из Инета и обеспечить доступ к ней.
program qtest; uses mysql4; Const DataBase : Pchar = '73113-tut-su'; Query : Pchar = 'Select * from un_forum'; Host : Pchar = 'localhost'; Login : Pchar = 'root'; var count,num : longint; code : integer; sock : PMYSQL; qmysql : TMYSQL; qbuf : string [160]; rowbuf : TMYSQL_ROW; dummy,host2,log2,pass2,ququ : string; recbuf : PMYSQL_RES; tt: text; begin { write('Host-> '); // Эти переменные можно ввести с клавиатуры readln(host2); Host:= PChar(host2); write('Login -> '); readln(log2); Login:= PChar(log2); write('Password -> '); readln(pass2); Password:= PChar(log2); write('DataBase -> '); readln(dummy); DataBase:= PChar(dummy); write('Query -> '); readln(ququ); Query:= PChar(ququ); } Write ('Connecting to MySQL...'); mysql_init(PMySQL(@qmysql)); sock := mysql_real_connect(PMysql(@qmysql),Host,Login,'',DataBase,0,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); assign(tt,'testdb4.txt'); rewrite(tt); while (rowbuf <>nil) do begin Write (tt,'(Id: ', rowbuf[0]); Write (tt,', Nik: ', rowbuf[1]); Write (tt,', messa : ', rowbuf[2],')'); Writeln(tt,', dtime : ', rowbuf[3],')'); rowbuf := mysql_fetch_row(recbuf); end; writeln(tt,'----- Конец --------'); close(tt); Writeln ('Freeing memory occupied by result set...'); mysql_free_result (recbuf); Writeln ('Closing connection with MySQL. Press Enter'); mysql_close(sock); readln; halt(0); end.
E:\XRAN_RW_MyProg\FPC_FPC>testdb4.exe Connecting to MySQL...Done. Connection data: Host info : localhost via TCP/IP Server info : Uptime: 280 Threads: 1 Questions: 1 Slow queries: 0 Ope 12 Flush tables: 1 Open tables: 3 Queries per second avg: 0.004 Client info : 5.0.45 Selecting Database 73113-tut-su... Executing query : Select * from un_forum... Number of records returned : 2 Number of fields per record : 4 Freeing memory occupied by result set... Closing connection with MySQL. Press Enter