* * *

Author Topic: Hello World  (Read 577 times)

rgh

  • New member
  • *
  • Posts: 33
Hello World
« on: May 17, 2017, 10:01:23 pm »
I've just downloaded Lazarus 1.6.4 & am trying to use fpc from the command line.

I'm on windows & have

E:\lazarus\fpc\3.0.2\bin\i386-win32

on my path. Hello World, that is:

Code: Pascal  [Select]
  1. program Hello;
  2. begin
  3.   writeln ('Hello, world.')
  4. end.

compiles ok. However, trying something slightly more complicated:

Code: Pascal  [Select]
  1. Program ConnectDB;
  2.  
  3. function CreateConnection: TIBConnection;
  4. begin
  5.   result := TIBConnection.Create(nil);
  6.   result.Hostname := 'localhost';
  7.   result.DatabaseName := '/opt/firebird/examples/employee.fdb';
  8.   result.UserName := 'sysdba';
  9.   result.Password := 'masterkey';
  10. end;
  11.  
  12. var  
  13.   AConnection : TIBConnection;
  14.  
  15. begin
  16.   AConnection := CreateConnection;
  17.   AConnection.Open;
  18.   if Aconnection.Connected then
  19.     writeln('Successful connect!')
  20.   else
  21.     writeln('This is not possible, because if the connection failed, ' +
  22.             'an exception should be raised, so this code would not ' +
  23.             'be executed');
  24.   AConnection.Close;
  25.   AConnection.Free;
  26. end.
  27.  

results in compiler errors, starting with:

connectdb.pas(3,41) Error: Identifier not found "TIBConnection"

I assume there is a pas or pp source file somewhere in the lazarus installation, the location of which I have to give to the compiler. Could anyone tell me how I tell the compiler where to look?

SkyKhan

  • Full Member
  • ***
  • Posts: 166
Re: Hello World
« Reply #1 on: May 17, 2017, 10:44:25 pm »
TIBConnection is not defined system-wide. It is defined in a unit named ibconnection;
So you need to add  after "Program ConnectDB;"
Code: Pascal  [Select]
  1. {$MODE DELPHI} // or {$MODE OBJFPC}
  2. {$H+} // this tells to compiler to use long strings as default. Most probably you are going to need it.
  3.  
  4. uses
  5.   IBConnection;
  6.  

I'm not here to learn but to help beginners mostly. So you dont really need to poke into my answers if you'll try to be pedantic. Really. If you really need to teach something , you can speak to questioners, not me. Thanks in advance.

rgh

  • New member
  • *
  • Posts: 33
Re: Hello World
« Reply #2 on: May 17, 2017, 11:50:45 pm »
Yep, that compiles!
Thanks SkyKhan.

That source was from the wiki:

http://wiki.freepascal.org/SqlDBHowto#How_to_connect_to_a_database_server.3F

so I'd assumed it was going to work as is.

Zoran

  • Hero Member
  • *****
  • Posts: 1131
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Hello World
« Reply #3 on: May 18, 2017, 07:18:14 am »
Yep, that compiles!
Thanks SkyKhan.

That source was from the wiki:

http://wiki.freepascal.org/SqlDBHowto#How_to_connect_to_a_database_server.3F

so I'd assumed it was going to work as is.

Wiki example fixed now.
@rgh, Wiki is edited by community, so when you encounter something wrong on wiki, feel free to update it yourself.
« Last Edit: May 18, 2017, 07:22:48 am by Zoran »

rgh

  • New member
  • *
  • Posts: 33
Re: Hello World
« Reply #4 on: May 18, 2017, 08:40:28 am »
In that case, the second example on that same wiki page

http://wiki.freepascal.org/SqlDBHowto#How_to_execute_direct_queries.2Fmake_a_table.3F

is also missing uses statements. IBConnection I now know about, but the next missing unit is whatever provides TSQLTransaction. In a case such as this, where do I look up what unit contains a given class?

when you encounter something wrong on wiki, feel free to update it yourself.

I don't feel confident enough in the language yet to start editing the wiki. In this case for example, I was wondering if compilation would succeed if done from within the Lazarus ide, although trying it myself, the answer seems to be no.


Zoran

  • Hero Member
  • *****
  • Posts: 1131
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Hello World
« Reply #5 on: May 18, 2017, 09:10:54 am »
is also missing uses statements. IBConnection I now know about, but the next missing unit is whatever provides TSQLTransaction. In a case such as this, where do I look up what unit contains a given class?

When you don't know where to find it, you can use Code Browser (main menu->View->Code Browser), see attached image. So, it is in sqldb unit.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 582
    • Burdjia
Re: Hello World
« Reply #6 on: May 18, 2017, 09:12:52 am »
where do I look up what unit contains a given class?
Since the full documentation is on the web you can use a web search to find it.  For example, finding TSQLTransaction, click the first result, then look on the top-left: it says it's in unit sqldb of the FCL library.

rgh

  • New member
  • *
  • Posts: 33
Re: Hello World
« Reply #7 on: May 18, 2017, 01:51:07 pm »
Thanks for your help guys.
Think I've got the idea now.

avra

  • Hero Member
  • *****
  • Posts: 925
    • Additional info
Re: Hello World
« Reply #8 on: May 18, 2017, 05:49:51 pm »
Alternatively you can use also grep, PowerGrep or even something like TotalCommander to search for all files of *.pas, *.pp, *.inc and *.lpr type in your FPC/Laz dir that contain specific keyword you're looking for.
ct2laz - Easily convert components and projects between Lazarus and CodeTyphon

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus