Recent

Author Topic: [SOLVED] LAMW - App crash with TMySQL57ConnectionBridge  (Read 821 times)

Alcatiz

  • New member
  • *
  • Posts: 9
[SOLVED] LAMW - App crash with TMySQL57ConnectionBridge
« on: May 12, 2024, 10:27:23 am »
Hi all!

I'm trying to make a simple app to handle a MySQL/MariaDB database stored on my local network. The app can connect to the database but as soon as I try to activate the SQLTransaction the app crashes:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. begin
  3.   MySQL57ConnectionBridge1.HostName := '192.168.1.40';
  4.   MySQL57ConnectionBridge1.Port := 3306;
  5.   MySQL57ConnectionBridge1.DatabaseName := 'xxx';
  6.   MySQL57ConnectionBridge1.UserName := 'xxx';
  7.   MySQL57ConnectionBridge1.Password := 'xxx';
  8.   if MySQL57ConnectionBridge1.Connect
  9.      then
  10.        begin
  11.          EditText1.AppendLn('Connected!');
  12.          if MySQL57ConnectionBridge1.SQLTransaction.Active
  13.             then
  14.               EditText1.AppendLn('Transaction is active!')
  15.             else
  16.               EditText1.AppendLn('Transaction is NOT active...');
  17.          // MySQL57ConnectionBridge1.SQLTransaction.Active := True;   (* CRASH *)
  18.        end
  19.      else
  20.        EditText1.AppendLn('Error...');
  21. end;

When the last line is commented, the app returns "Connected!" and "Transaction is NOT active...", so it seems the SQLTransaction is correctly allocated.
If I replace the TMySQL57ConnectionBridge component with SQLDB components (TMySQL57Connection, TSQLTransaction and TSQLQuery) the app also crashes.
The same app compiled on Linux on another PC connected to the local network doesn't encounter any problem.
Connection to the database on my smartphone with any MySQL client is successfull.

So any ideas? Thx!  :)
« Last Edit: May 24, 2024, 09:21:48 am by Alcatiz »

biologic

  • Newbie
  • Posts: 4
Re: LAMW - App crash with TMySQL57ConnectionBridge
« Reply #1 on: May 12, 2024, 08:17:52 pm »
Hi.

Maybe you should put a begin-end statement around your instructions.

Just a guess.


Code: [Select]
if MySQL57ConnectionBridge1.Connect
     then
       begin
         EditText1.AppendLn('Connected!');
         if MySQL57ConnectionBridge1.SQLTransaction.Active
            then
              EditText1.AppendLn('Transaction is active!')
            else
              begin
                   EditText1.AppendLn('Transaction is NOT active...');
                   MySQL57ConnectionBridge1.SQLTransaction.Active := True;   (* CRASH *)
               end
       end
     else
       EditText1.AppendLn('Error...');


jmpessoa

  • Hero Member
  • *****
  • Posts: 2305
Re: LAMW - App crash with TMySQL57ConnectionBridge
« Reply #2 on: May 16, 2024, 03:45:45 am »

Hi, Alcatiz!

TMySQL57ConnectionBridge  fail to connect.....  but I can't fix it. Can you help?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Alcatiz

  • New member
  • *
  • Posts: 9
Re: LAMW - App crash with TMySQL57ConnectionBridge
« Reply #3 on: May 20, 2024, 10:22:09 am »
Just with tiny changes in the constructor and without allocating any transaction in the Connect() method, I could execute some queries on my database.  8-)

Here's my component version (I added error codes but they are certainly useless):
Code: Pascal  [Select][+][-]
  1. unit MySQLAndroidConnection;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources,
  9.   SQLDB, DB, MySQL57Conn;
  10.  
  11. type
  12.  
  13.   { TMySQL57ConnectionBridgeError }
  14.  
  15.   TMySQL57ConnectionBridgeError = (err_NoError, err_CreateSQLConnection, err_CreateSQLTransaction, err_CreateSQLQuery);
  16.  
  17.   { TMySQL57ConnectionBridge }
  18.  
  19.   TMySQL57ConnectionBridge = class(TComponent)
  20.   private
  21.     FDataBaseName: String;
  22.     FHostName: String;
  23.     FPort: Integer;
  24.     FPassword: String;
  25.     FUserName: String;
  26.     FError: TMySQL57ConnectionBridgeError;
  27.     procedure SetDataBaseName(AValue: String);
  28.     procedure SetHostName(AValue: String);
  29.     procedure SetPassword(AValue: String);
  30.     procedure SetPort(AValue: Integer);
  31.     procedure SetUserName(AValue: String);
  32.   public
  33.     MySQL57Connection: TMySQL57Connection;
  34.     SQLQuery: TSQLQuery;
  35.     SQLTransaction: TSQLTransaction;
  36.     constructor Create(AOwner: TComponent); override;
  37.     destructor Destroy; override;
  38.     function Connect: Boolean;
  39.   published
  40.     property DataBaseName: String read FDataBaseName write SetDataBaseName;
  41.     property HostName: String read FHostName write SetHostName;
  42.     property Port: Integer read FPort write SetPort;
  43.     property Password: String read FPassword write SetPassword;
  44.     property UserName: String read FUserName write SetUserName;
  45.     property Error: TMySQL57ConnectionBridgeError read FError;
  46.   end;
  47.  
  48. procedure Register;
  49.  
  50. implementation
  51.  
  52. procedure Register;
  53. begin
  54.   {$I mysqlandroidconnection_icon.lrs}
  55.   RegisterComponents('FCL Bridges', [TMySQL57ConnectionBridge]);
  56. end;
  57.  
  58. { TMySQL57ConnectionBridge }
  59.  
  60. procedure TMySQL57ConnectionBridge.SetDataBaseName(AValue: String);
  61. begin
  62.   if FDataBaseName = AValue then Exit;
  63.   FDataBaseName := AValue;
  64. end;
  65.  
  66. procedure TMySQL57ConnectionBridge.SetHostName(AValue: String);
  67. begin
  68.   if FHostName = AValue then Exit;
  69.   FHostName := AValue;
  70. end;
  71.  
  72. procedure TMySQL57ConnectionBridge.SetPassword(AValue: String);
  73. begin
  74.   if FPassword = AValue then Exit;
  75.   FPassword := AValue;
  76. end;
  77.  
  78. procedure TMySQL57ConnectionBridge.SetPort(AValue: Integer);
  79. begin
  80.   if FPort = AValue then Exit;
  81.   FPort := AValue;
  82. end;
  83.  
  84. procedure TMySQL57ConnectionBridge.SetUserName(AValue: String);
  85. begin
  86.   if FUserName = AValue then Exit;
  87.   FUserName := AValue;
  88. end;
  89.  
  90. constructor TMySQL57ConnectionBridge.Create(AOwner: TComponent);
  91. (* Allocates SQLDB components *)
  92. begin
  93.   inherited Create(AOwner);
  94.   FError := err_NoError;
  95.   try
  96.     MySQL57Connection := TMySQL57Connection.Create(Nil);
  97.     MySQL57Connection.SkipLibraryVersionCheck := True;
  98.   except
  99.     FError := err_CreateSQLConnection;
  100.     Exit;
  101.   end;
  102.   try
  103.     SQLTransaction := TSQLTransaction.Create(Nil)
  104.   except
  105.     FError := err_CreateSQLTransaction;
  106.     Exit;
  107.   end;
  108.   try
  109.     SQLQuery := TSQLQuery.Create(Nil);
  110.   except
  111.     FError := err_CreateSQLQuery;
  112.   end;
  113. end;
  114.  
  115. destructor TMySQL57ConnectionBridge.Destroy;
  116. (* Deallocates SQLDB components *)
  117. begin
  118.   if Assigned(SQLQuery)
  119.      then
  120.        begin
  121.          SQLQuery.Close;
  122.          SQLQuery.Free;
  123.        end;
  124.   if Assigned(SQLTransaction)
  125.      then
  126.        begin
  127.          SQLTransaction.Active := False;
  128.          SQLTransaction.Free;
  129.        end;
  130.   if Assigned(MySQL57Connection)
  131.      then
  132.        begin
  133.          MySQL57Connection.Connected := False;
  134.          MySQL57Connection.Free;
  135.        end;
  136.   inherited Destroy;
  137. end;
  138.  
  139. function TMySQL57ConnectionBridge.Connect: Boolean;
  140. (* Tries to connect to database *)
  141. begin
  142.   Result := False;
  143.   MySQL57Connection.DatabaseName := FDataBaseName;
  144.   MySQL57Connection.HostName := FHostName;
  145.   MySQL57Connection.Port := FPort;
  146.   MySQL57Connection.Password := FPassword;
  147.   MySQL57Connection.UserName := FUserName;
  148.   MySQL57Connection.Transaction := SQLTransaction;
  149.   MySQL57Connection.CharSet := 'UTF8';
  150.   SQLQuery.DataBase := MySQL57Connection;
  151.   try
  152.     MySQL57Connection.Open;
  153.     MySQL57Connection.Connected := True;
  154.   except
  155.     MySQL57Connection.Connected := False;
  156.   end;
  157.   Result := MySQL57Connection.Connected;
  158. end;
  159.  
  160. end.

And this code worked:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.btnConnectClick(Sender: TObject);
  2. (* Connection to the database *)
  3. begin
  4.   EditText1.Append('Creation result: ');
  5.   case MySQLAndroidConnection1.Error of
  6.     err_NoError: EditText1.AppendLn('no error');
  7.     err_CreateSQLConnection: EditText1.AppendLn('err_CreateSQLConnection');
  8.     err_CreateSQLTransaction: EditText1.AppendLn('err_CreateSQLTransaction';
  9.     err_CreateSQLQuery: EditText1.AppendLn('err_CreateSQLQuery');
  10.   end;
  11.   MySQLAndroidConnection1.HostName := '192.168.1.40';
  12.   MySQLAndroidConnection1.Port := 3306;
  13.   MySQLAndroidConnection1.DatabaseName := 'xxxxxxx';
  14.   MySQLAndroidConnection1.UserName := 'yyyyyyy';
  15.   MySQLAndroidConnection1.Password := 'zzzzzzz';
  16.   if MySQLAndroidConnection1.Connect
  17.      then
  18.        EditText1.AppendLn('Connected!');
  19.      else
  20.        EditText1.AppendLn('Error...');
  21. end;
  22.  
  23. procedure TAndroidModule1.btnQueryClick(Sender: TObject);
  24. (* Lists a table *)
  25. begin
  26.   MySQLAndroidConnection1.SQLQuery.Close;
  27.   MySQLAndroidConnection1.SQLQuery.SQL.Text := 'SELECT * FROM T_CATEGORIES;';
  28.   MySQLAndroidConnection1.SQLQuery.Open;
  29.   MySQLAndroidConnection1.SQLQuery.Last;
  30.   EditText1.AppendLn('Number of records: ' + IntToStr(MySQLAndroidConnection1.SQLQuery.RecordCount));
  31.   MySQLAndroidConnection1.SQLQuery.First;
  32.   while not MySQLAndroidConnection1.SQLQuery.EOF do
  33.     begin
  34.       EditText1.AppendLn(MySQLAndroidConnection1.SQLQuery.FieldByName('F_CATEGORYNAME').AsString);
  35.       MySQLAndroidConnection1.SQLQuery.Next;
  36.     end;
  37. end;
« Last Edit: May 20, 2024, 10:24:02 am by Alcatiz »

 

TinyPortal © 2005-2018