SQL and ListCtrl Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

SQL and ListCtrl

Post by Ugly! »

1) What is the right way to work with a PostgreSql/Sqlite3 database (using DatabasLayer)...should i connect to the db every time i query and do modifications, or should i connect when the program starts and close the connection at exit???

2) I want to display the results of a VIEW in ListCtrl in Report mode, often a row is updated or inserted. how should i display the changes? update or insert the row in the ListCtrl? reload the VIEW? Is there any point in using a Virtual listCtrl?

3) I m thinking in setting the row clientdata to the an id in the database for easy dialog based modification...is this a correct aproach??? By the way, the VIEW has a WHERE item.id=... condition.


Thanks in advanced, Regards, Matias.

PS: i a newbie when it comes to SQL, and combining it with a program, so i'm sorry if this questions seems to dull.
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
jb_coder
Super wx Problem Solver
Super wx Problem Solver
Posts: 267
Joined: Mon Oct 18, 2004 10:55 am

Post by jb_coder »

For part 1, I'd recommend keeping a connection for as long as you know that you'll need to access it. Be sure to close any result sets and prepared statements as soon as you're done with them though.

I don't have any answers for the other 2 questions.
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

Post by Ugly! »

jb_coder wrote:For part 1, I'd recommend keeping a connection for as long as you know that you'll need to access it. Be sure to close any result sets and prepared statements as soon as you're done with them though.
how should i create this connection??? Should i pass it as a parameter in every frame or dialog that should connect to the database? Should i create a global variable for the connection? with extern declaration?

Thnx in advanced, Matias
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
jb_coder
Super wx Problem Solver
Super wx Problem Solver
Posts: 267
Joined: Mon Oct 18, 2004 10:55 am

Post by jb_coder »

In an application that I wrote, the main application class help the connection and the exposed function was GetDatabaseLayer() which returned a pointer to the database connection. Other methods that I've seen before include having a connection pool class which was used to get a shared connection to the database. A third option that could be used (if you're using a client/view style) is to store the database connection in the client class. That also might make it easy to determine when to have a database connection open. If you don't have a client open, then you wouldn't have a database connection.

Here are the easiest ways that I know of to create database connections:

1) SQLite:

Code: Select all

DatabaseLayer* pDatabase = new SqliteDatabaseLayer(_("data.db"));
2) PostgreSQL:

Code: Select all

DatabaseLayer* pDatabase = new PostgresDatabaseLayer(_("localhost"), _("database"), _("user"), _("password"));
;

You can find all the possible constructors on the doxygen pages:
http://wxcode.sourceforge.net/docs/data ... Layer.html
http://wxcode.sourceforge.net/docs/data ... Layer.html
Ugly!
Earned some good credits
Earned some good credits
Posts: 113
Joined: Mon May 09, 2005 5:11 am
Location: Argentina - BS AS

Post by Ugly! »

I haven't started with the database stuff yet, i'm just looking around for the best way to aproach this. I think what I'm going to do is to create the connection at startup and then create a method for each, frame or dialog to get the connection pointer from its parent. My program has a lot of nested dialogs and frames wich connect to the same database, so I think this might the best way to go. No point in creating the database connection each time.
Then i'll close the connection on exit time.

If any ones thinks this is a terrible idea or can think of a better way, plz let me know.

Regards, Mat
Just a newbie - Too many interests, not too many time.

Windows XP SP2
Kubuntu GNU/Linux - Feisty
wxActiveRecordGenerator (aka wxARG) maintainer
Find it at wxCode
Post Reply