Recent

Author Topic: Attaching to a mysql Server  (Read 3935 times)

thaimann

  • Jr. Member
  • **
  • Posts: 84
Attaching to a mysql Server
« on: December 02, 2007, 06:07:31 pm »
I do now have a text program able to access a mysql 5.0 server.  It does behave kind of strange though.  After I intialize the mysql data structures, writeln doesn't work unless I have it going out to stderr.  That is what was causing that error in my previous post.

I suspect that when initizing the data structures some memory is getting overwritten.  I have another program that isn't a toy program that I converted from mysql ver 4, which accepts some parms from command line.  Those values become unreadable after initializng the data structures.

I do a "mysql_init(PMySQL(@qmysql));" just like I used to, but in the "mysql_real_connect" I hard code the database and skip the "mysql_select_db" function, I could not get "mysql_select_db" function to work.

The type TMySQL becomes PMySQL and
                TMySQL_Row becomes MySQL_Row

Well, whatever here is my source code:

Program MysqlTest;

uses
  sysutils, strings, mysql50;
 
Var
  sock      : PMYSQL;
  qmysql    : Mysql_Res;
  rowbuf    : Mysql_ROW;
  recbuf    : PMYSQL_RES;
  SqlStmt   : PChar;
  i, reccnt   : Integer;

Begin
   SqlStmt := StrAlloc(200 * Sizeof(char));
   WriteLn('Begin');
   WriteLn('Connecting to MySQL...');
   mysql_init(PMySQL(@qmysql));
   sock :=  mysql_real_connect(PMysql(@qmysql),nil,'user','password','database',0,nil,0);
   WriteLn(stderr, 'Connect');
   if sock=Nil then
   Begin
       Writeln (stderr, 'Couldn''t connect to MySQL.');
       Writeln (stderr, mysql_error(@qmysql));
       halt(1);
   end;
   SqlStmt := 'Select LocIdx, Location, TZone from location order by LocIdx';
   if (mysql_query(sock,SqlStmt) < 0) then
   Begin
      Writeln (stderr,'Query failed ');
      writeln (stderr,mysql_error(sock));
      Halt(1);
   End;
  recbuf := mysql_store_result(sock);
  reccnt := mysql_num_rows(recbuf);
  if (reccnt = 0) then
  Begin
    Writeln (stderr,'Query returned nil result.');
    mysql_close(sock);
    halt (1);
  End;
  Writeln (stderr,'Number of records returned  : ', reccnt);
  for i:=1 to reccnt Do
  Begin
        rowbuf := mysql_fetch_row(recbuf);
       WriteLn(stderr,rowbuf[1], rowbuf[2], rowbuf[3]);
  End;
  mysql_free_result (recbuf);
  mysql_close(sock);
End.

 

TinyPortal © 2005-2018