Recent

Author Topic: Problem with connecting to MariaDB 5.5  (Read 2645 times)

tormods

  • New Member
  • *
  • Posts: 17
Problem with connecting to MariaDB 5.5
« on: April 21, 2014, 09:37:23 pm »
I had a problem with connecting to MariaDB 5.5 using TMySQL55Connection. First problem was missing libmysql.dll
I got a lot of suggestions from the community, to make it short: placing the file in sysWOW64 did the trick since
my PC is 64bit running windows 8.
Now I am trying to get some data from the database, but have no success. This is my code:

procedure TSkrDatabaseConnector.__SkrDBConnect( aOwner : TComponent );

begin
  _ConnectState           := false;
  _Connector              := TMySQL55Connection.Create(aOwner);
  _Connector.HostName     := _DBHostName; // 127.0.0.1
  _Connector.UserName     := _DBUserName; // root
  _Connector.Password     := _DBPassword; // abc
  _Connector.DatabaseName := _DBDatabaseName; // skrcontractlogger
  _Connector.Port         := _DBPort; // 3333
  _Connector.CharSet      := 'utf8';
  _Connector.Open;
  if (_Connector.Connected) then begin // program comes here setting _ConnectState to true
    _ConnectState         := true;
    _Transaction          := TSQLTransaction.Create(aOwner); // set up transaction
    _Transaction.Database := _Connector;
    _Query                := TSQLQuery.Create(aOwner); // set up sql query
    _Query.Database       := _Connector;
    _Query.Transaction    := _Transaction;
    _DataSource           := TDataSource.Create(aOwner); // set up data source
    _DataSource.DataSet   := _Query;
  end else _Connector.Free;
end;

function TSkrDatabaseConnector.__SkrDBQuery(aSQLText : string) : TSQLQuery; // do query

begin
  _Connector.Open;
  _Transaction.StartTransaction;
  _Query.SQL.Text := aSQLText;
  _Query.Open;
  Result := _Query;
  _Query.Close;
end;

// code above in a separate object, this is calling code

...
_Connect := TSkrDatabaseConnector.Create(Form1,Form1,400,200,
                                             'localhost',
                                             'SkrContractLogger',
 // calling constructor                      'root',
 // which calls __SkrDBConnect               'abc',
                                             3333);
    _Grid := TDBGrid.Create(Form1); // set up grid
    _Grid.DataSource := _Connect.dbDataSource;
    _Grid.Visible := true;
    _Grid.Enabled := true;
    _Grid.Parent := Form1;
    query := _Connect.__SkrDBQuery('SELECT * FROM users');
                                                           
Everything compiles OK and runs well - but no data shows, the table users contain rows of users with
names, e-mailaddresses etc. I am running Lazarus and MariaDB both in 32 bit versions on a 64 bit PC with
Windows 8. I tried to collect recordlength and got the value 10, which is default. Tried to move to last
record to get number of records in db - program terminated on an exception, what is wrong?  Is there any
way to test results from connection.

eara

  • Jr. Member
  • **
  • Posts: 84
Re: Problem with connecting to MariaDB 5.5
« Reply #1 on: April 22, 2014, 12:13:30 am »
1. have you tried this code with other db ?
2. in function TSkrDatabaseConnector.__SkrDBQuery(aSQLText : string) : TSQLQuery
Code: [Select]
_Query.Open;
  Result := _Query;
 _Query.Close; // <<<<<<<<<<<<<<<<<
seems that you return a "closed" query....



tormods

  • New Member
  • *
  • Posts: 17
Re: Problem with connecting to MariaDB 5.5
« Reply #2 on: April 22, 2014, 09:04:10 am »
I have searched for problems elsewhere, closing the Query was not a good idea - it now works as it should ... Thank you

eara

  • Jr. Member
  • **
  • Posts: 84
Re: Problem with connecting to MariaDB 5.5
« Reply #3 on: April 22, 2014, 11:33:46 am »
seems you thought "lets get the resultset (like in ado) and close the resource in order not to waste db resources..."
well the resultset is the query... so closing it is made it useless.
Also, recordcount was partial calculated (in my cases if recordcount is not right, so i do an .Last after open and then return to the current pos in order to calculate the right recordcount, but this is resource consuming and should be avoided if not needed i think)...

happy that i helped...

 

TinyPortal © 2005-2018