Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-03

Writing a Console App in FreePascal that connects to MySQL

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Terry A. Haimann

I was trying to see how to write a Console Appication in FreePascal that will connect to MySQL. I know how to do this in c, but thought it would be interesting to be able to do it in Pascal.

How should FConnection and FTransaction be declared or are the declared in a library somewhere?

I have the following source code which is based on the web site http://wiki.freepascal.org/SqlDBHowto, but does not compile:

Program TestMysql;
Uses sqldb, mysql55conn;
Var
        MyConnection:   TSQLConnector;
        MyTransaction:  TSQLTransaction;
        MyQuery:                TSQLQuery;
function GetQuery : TSQLQuery;
  var MyQuery : TSQLQuery;
begin
  MyQuery := TSQLQuery.Create;
  MyQuery.Database := FConnection;
  MyQuery.Transaction := FTransaction;
  GetQuery := MyQuery;
end;
procedure CreateTransaction;
begin
  MyTransaction := TSQLTransaction.Create;
  MyTransaction.Database := MyConnection;
end;
Procedure CreateConnection;
Begin
        MyConnection := TSQLConnector.Create(nil);
        MyConnection.ConnectorType      := 'MySQL 5.5';
        MyConnection.Hostname           := '127.0.0.1';
        MyConnection.DatabaseName       := 'MyDB';
        MyConnection.UserName           := 'MyUser';
        MyConnection.Password           := 'MyPassword';
End;
Begin
        CreateConnection;
        CreateTransaction;
        MyQuery := GetQuery;
        MyQuery.SQL.Text := 'select ObjIdx, ObjCon from object' +
                'Order By ObjCon, ObjIdx';
        MyConnection.Open;
        MyQuery.Open;
        If MyConnection.Connected Then
        Begin
                While Not MyQuery.eof Do
                Begin
                        WriteLn(MyQuery.FieldByName('ObjIdx').AsString,
                                MyQuery.FieldByName('ObjCon').AsString);
                        MyQuery.Next;
                End;
        End Else WriteLn('Failure');
        MyQuery.Close
        MyConnection.Close;
        MyQuery.Free
        MyConnection.Free;
        MyTransaction.Free;
End.

Reply by email