how to set database ip and login

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
guzzi_jones
Experienced Solver
Experienced Solver
Posts: 81
Joined: Sun Dec 08, 2013 3:50 am

how to set database ip and login

Post by guzzi_jones » Mon Sep 29, 2014 12:27 am

What I want is a dialog that sets the ip address of my database connection.
What is the best way to implement a dialog to set the ip address of the database to connect to.
I have created a wxDialog that has a text box for the server ip address.
I have a class that actually does the database connection called dbcon.h and shown it below.

What is the best method to get the variables over to this class when I invoke a connection.
I was thinking to create a class called server_parms.h that has getter and setter for server_ip.

I would need to pass this to each dialog though right? This cause me to have to edit every single new window call and pass the additional object.
it seems like there should be an easier way to do this.

Is a macro the best solution?
Edit: This is something I would like the user's system to remember. Would it be best to write a config file to their pc and read from that file in my dbcon.h?
Any help or pointers appreciated on some classes to do this.
Thanks I am a newb.

Code: Select all

#ifndef DBCON_H
#define DBCON_H

#include <pqxx/pqxx>
#include <iostream>
#include <string>
#include <iostream>
#include <sstream>
#include <wx/msgdlg.h>
#include <wx/choice.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>

using namespace std;
using namespace pqxx;
class dbcon
{
    connection*  C;
    public:
    //explicitly declare constructor
    dbcon();
    ~dbcon();
    int SetConnection(){
        std::string result_text;
       C= new pqxx::connection("dbname=distdata user=redmine password=xxxx hostaddr=192.168.1.104 port=5432");
      if (C->is_open()) {
         result_text = "Opened database successfully: ";
         return 0;
      } else {
         result_text = "Can't open database";
         return 1;
      }
    }
   pqxx::result querry(std::string strSQL){
        //SetConnection();

        pqxx::work trans(*C,"trans");
        pqxx::result res;

        res=trans.exec(strSQL);
        trans.commit();
        return res;
    }
    void Disconnect(){
          C->disconnect ();
    }
};
#endif // DBCON_H

User avatar
T-Rex
Moderator
Moderator
Posts: 1198
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: how to set database ip and login

Post by T-Rex » Mon Sep 29, 2014 11:27 am

If your dbcon class represents the database connection (btw, probably would be better to use DatabaseLayer for this), then you need to create a constructor with parameters: hostname, port, database name, login, password. Then create a dialog class. The dialog class will contain variables (wxString for hostname, database name, login, password; wxUint32 for port number), attach these variables to text fields in dialog using wxGenericValidator. Then, when you create the dialog, you need to set these variables in code, call ShowModal() for dialog. Process the result of ShowModal() and get the values of these variables if ShowModal() returned wxID_OK. Then pass these values to the constructor of your dbcon class.

The best way to see this approach in action is using DialogBlocks (maybe a trial version). You can take a look at the source code which it generates and do the same in your app (or just re-use the dialog class created by DialogBlocks).

Again, would be better to use the DatabaseLayer, it is functional enough and simplifies the API for database access.
http://wxwidgets.info/tag/databaselayer/ (you can use Google Translate for English version)

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: how to set database ip and login

Post by PB » Mon Sep 29, 2014 1:13 pm

If you want to store some configuration data on a user's machine, then checking out wxConfig and related classes may be a good idea.

guzzi_jones
Experienced Solver
Experienced Solver
Posts: 81
Joined: Sun Dec 08, 2013 3:50 am

Re: how to set database ip and login

Post by guzzi_jones » Tue Sep 30, 2014 3:16 pm

T-Rex wrote:If your dbcon class represents the database connection (btw, probably would be better to use DatabaseLayer for this), then you need to create a constructor with parameters: hostname, port, database name, login, password. Then create a dialog class. The dialog class will contain variables (wxString for hostname, database name, login, password; wxUint32 for port number), attach these variables to text fields in dialog using wxGenericValidator. Then, when you create the dialog, you need to set these variables in code, call ShowModal() for dialog. Process the result of ShowModal() and get the values of these variables if ShowModal() returned wxID_OK. Then pass these values to the constructor of your dbcon class.

The best way to see this approach in action is using DialogBlocks (maybe a trial version). You can take a look at the source code which it generates and do the same in your app (or just re-use the dialog class created by DialogBlocks).

Again, would be better to use the DatabaseLayer, it is functional enough and simplifies the API for database access.
http://wxwidgets.info/tag/databaselayer/ (you can use Google Translate for English version)

Ok I am following you so far and I do have the above already.
My question is now how do i pass the variables :hostname, port, database name, login, password to a new dialog window without having to add parameters to each new dialog window constructor?

I basically want these variables available in child dialogs.
Thanks I am new so i am still learning.
If there is anything I can send along let me know.

User avatar
T-Rex
Moderator
Moderator
Posts: 1198
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: how to set database ip and login

Post by T-Rex » Tue Sep 30, 2014 5:31 pm

Well, I assume that you have a dialog class and several member variables in it. You then will need to create getter and setter methods for each such variable. Then, when you create your dialog object, ad before you call ShowModal(), you can set the variable values by calling these setter methods. And if you attached these variables to text fields using validators, their values will be automatically displayed in text fields when the dialog gets shown. Also, these variables will contain the values which you entered in text fields, after ShowModal() finishes its work.

guzzi_jones
Experienced Solver
Experienced Solver
Posts: 81
Joined: Sun Dec 08, 2013 3:50 am

Re: how to set database ip and login

Post by guzzi_jones » Mon Oct 06, 2014 1:45 am

Ok i have added the validator code to my TextCtrl and i am getting the compile error:

Code: Select all

/home/ajonen/distdata/bdn/stable/server.cpp|39|error: ‘wxFILTER_ALPHA’ was not declared in this scope|
/home/ajonen/distdata/bdn/stable/server.cpp|39|error: ‘wxTextValidator’ was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
Here is the snippet:

Code: Select all

	TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize(110,27), 0, wxTextValidator(wxFILTER_ALPHA,name), _T("ID_TEXTCTRL1"));
What am i missing here? i added the header file

Code: Select all

#include <wx/validate.h>
Thanks, from a noob

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: how to set database ip and login

Post by doublemax » Mon Oct 06, 2014 10:41 am

Use the source, Luke!

Post Reply