Lazarus
Programming => Networking and Web Programming => Topic started by: jamhitz on January 30, 2013, 07:51:28 pm
-
Team,
I have been playing around with Brook Framework and I must say I am thoroughly impressed so far!! 8-)
I have a question: Most (all?) of the demos have a SqlLite3 schema and implementation; a number may have PostgreSQL schemas but no implementation; nothing at all for the other DBMS... but of course the whole point of a demo is to pick up from there and figure it out. However, I cannot figure out how to use SqlDB to connect to any other database but SQLLite:
BrookSettings.Configuration := 'library=sqldb;driver=pqconnection;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
I have tried replacing the driver with PQ, Postgres, PostgreSQL, MySQL55, MySQL51.. (of course each to its respective server) nothing works. However dropping a TPQConnection or a TMysql5*conn on a form and setting the proper parameters works fine.
What am I missing?
Thanks.
Kind regars
JamHitz
-
Hello friend,
In brokers unit, just declare your connection unit, eg:
https://github.com/silvioprog/brook-site/blob/master/src/release/brokers.pas
Good luck! :)
-
... and change your configuration to:
BrookSettings.Configuration := 'library=sqldb;driver=postgresql;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
-
@SilvioProg
Eureka!!!! Can you see the smile? :) I am intent on writing a small (VEEEEERY basic) 101 tutorial on using Brook for web programming using FPC/Laz. I hope in this way I will play my little part in making Brook what I believe it has the potential to become - the number one Web Programming Framework for FPC/Laz. In the meantime I stumbling, falling and finding my way around using the demos and the documentation.
Thank you.
Kind regards
JamHitz
-
Very nice friend! I saw the smile. :)
What the link to the site where you will post? We may publish it in the official community of Lazarus on Facebook, GooglePlus etc.
-
I will most probably post it to the Lazarus wiki and/or on the BrookFramework website.
-
Great! ;)
It will be posted in future blog of Brook too! :-X
-
I can now be able to connect to a DB. However, attempting to use TBrookTable or TBrookQuery (by alternating the commented lines):
procedure TMyAction.Get;
var db: TBrookDatabase;
q: TBrookQuery;
t: TBrookTable;
begin
BrookSettings.Configuration := 'library=sqldb;driver=postgresql;host=SERVER;user=USER;password=PASS;database=db';
try
db := TBrookDatabase.Create;
//q := TBrookQuery.Create(db);
t := TBrookTable.Create(db, 'gateway');
try
WriteLn(t.AsJSON);
//q.SQL.Text := 'SELECT id,name,description from gateway';
//q.Open;
//WriteLn(q.AsJSON);
except
on E:Exception do
Write('ERROR:' + E.Message);
end;
finally
FreeAndNil(db);
FreeAndnil(q);
FreeAndNil(t);
end;
...brings up an error Premature end of script headers: cgi3 .
Ideas?
Thank you.
-
Don't use BrookSettings.Configuration in actions, use it on the brokers unit.
You can facilitate decreasing the coding:
...
TMyAction = class(TBrookDBAction)
...
procedure TMyAction.Get;
begin
Write(Table.Open.AsJSON); // or Table.AsJSON directly
end;
...
initialization
TMyAction.Register('gateway', '*');
-
@Silvio
I've created a brand new project that uses TBrookDBAction instead of TBrookAction, but I still get the same error. Here is the broker:
unit brokers007;
{$mode objfpc}{$H+}
interface
uses
BrookFCLCGIBroker, BrookUtils, BrookSQLdbBroker, PQConnection;
implementation
initialization
BrookSettings.Charset := 'library=sqldb;driver=postgresql;host=127.0.0.1;user=USERNAME;password=PASSWORD;database=TESTDB';
BrookSettings.Page404 := '404.html';
BrookSettings.Page500 := '500.html';
end.
..and here is the action:
unit umain007;
{$mode objfpc}{$H+}
interface
uses
BrookDBAction, fpJSON;
type
TMyAction = class(TBrookDBAction)
public
procedure Get; override;
end;
implementation
{ TMyAction }
procedure TMyAction.Get;
begin
Write(Table.Open.AsJSON);
end;
initialization
TMyAction.Register('gateway','*');
end.
See the attachment for the response.
JamHitz
-
Hello, sorry for my delay.
So, try to run the demo "\repository\git\brookframework\demos\simple\helloworld\cgi\cgi1.lpi". I need to find out if the error is in your DB or in your HTTP server.
-
Hi
I have tested MOST of the demos in the /simple, /http and /others sub-directories. They all work as expected. It is those in the /db sub-directory that are a problem.
OpenSuse 12/Lazarus 1.1/fpc 2.7/PostgreSQL 9.1/MySQL 5.5
JamHitz
-
You can connect using pure SQLdb? Take a test, add a SQLConnector1 in a form and try to connect.
-
Yes, a regular SQLdb-based desktop application works just fine accessing the very same database with the very same credentials.
In fact CGI applications based of fpWeb and FreeSpider are accessing the database ok.
-
I don't know what can be, because I could not reproduce the error.
Download this demo:
http://www.sendspace.com/file/wkajve
And follow the steps below:
1. # zypper install sqlite3-devel ;
2. Open and run the project "project1.lpi";
3. Call http://localhost in your browser;
This allow you to set a break point and debug your application, running the it step by step with F8.
-
Thank you. I can see some progress. When I compile the program and run it off the terminal, my browser is able to get the JSON object fetched from the database (I tested the DB using Database Desktop).
However, I cannot run the app from my IDE, since it is attempting to bind to Port 80, and my Lazarus does not run as root. How do I change the port on which the app binds so that I can debug the code by stepping using a regular user account?
JamHitz
-
Hm... without error in the embedded server, we will not need to debug with this technique. :)
Are you using apache? Port 80?
Execute the commands below:
# zypper install postgresql-devel
# zypper install sqlite3-devel
It will install client libs of your database servers.
-
Apache is installed, the *-devel packages are installed; and of course when testing the project, Apache is off, and the Firewall is off. But, because I am running this as a regular user, I cannot access port 80 (ports < 1024) when running from the Lazarus IDE. However, if I run the compiled program as root, I can then access my application on port 80.
What I need to do is to change the port to something like 8080 so that I can be able to debug the program.
I know I can set re-routing in the iptables but I was hoping to not have to do this.
Thank you.
-
Hello,
I think that I found your problem.
See this picture:
http://imagebin.org/246565
So, use BrookSettings.Configuration instead BrookSettings.Charset. :)
-
Hello Silvio
You are right. I think I trusted Code Assist a little too much to counter-check. It works ok now. I'll finally get the tutorial going. Thank you.
-
Nice! :)