Initialization of the process is described below.
[codesyntax lang=”cpp”]
void grailsGenFrame::OnmenOpt1Selected(wxCommandEvent& event) { db.listDatabases(); db.selDB(); }
[/codesyntax]
The definition for db is described below.
[codesyntax lang=”cpp”]
zDB01 db;
[/codesyntax]
The dialog must be initiated.
[codesyntax lang=”cpp”]
#ifndef ZDB01_H
#define ZDB01_H
#include <mysql++.h>
#include <wx/arrstr.h>
class zDB01
{
public:
zDB01();
virtual ~zDB01();
void listDatabases();
wxArrayString dbn;
void selDB();
wxString db;
protected:
private:
};
#endif // ZDB01_H
void zDB01::selDB() { dlgSelDB dlg(NULL,wxID_ANY); dlg.lbxDbNames->InsertItems(dbn, 0); if (dlg.ShowModal() == wxID_OK) { } }
[/codesyntax]
The dbn variable is defined here.
[codesyntax lang=”cpp”]
wxArrayString dbn;
[/codesyntax]
When a wxListBox is double clicked the following code will capture the selection and end modal dialog operation.
[codesyntax lang=”cpp”]
void dlgSelDB::OnlbxDbNamesDClick(wxCommandEvent& event) { buf = lbxDbNames->GetStringSelection().ToStdString(); EndModal(wxID_OK); }
[/codesyntax]
I created a new class which isolates the main project from database manipulations.
[codesyntax lang=”cpp”]
#include "zDB01.h" #include <mysql++.h> #include <wx/msgdlg.h> #include "dlgSelDB.h" using namespace mysqlpp; using namespace std; zDB01::zDB01() { //ctor } zDB01::~zDB01() { //dtor } void zDB01::listDatabases() { // dlgSelDB dlb; dbn.Clear(); try { Connection conn(true); conn.connect("bcswebtools", "localhost", "bcs", "Peace007!amb"); Query query = conn.query(); // query << "SELECT * FROM lk1 LIMIT 10"; query << "show DATABASES"; StoreQueryResult ares = query.store(); for (size_t i = 0; i < ares.num_rows(); i++) { string buf = std::string(ares[i]["Database"]); // wxMessageBox(buf); wxString tbuf = buf; dbn.Insert(tbuf,0,1); // lbxDbName.InsertItems(ares[i]["Database"]); // cout << "Name: " << ares[i]["idlk1"] << " - Address: " << ares[i]["cat"] << endl; } if (ares.num_rows() == 0) { wxMessageBox("No Data Detected!!", "Zero Query Results", wxICON_HAND); } } catch (BadQuery er) // handle any connection or { // query errors that may come up cerr << "Error: " << er.what() << endl; wxMessageBox("Connection Error!", "Connection Error Detected", wxICON_ERROR | wxICON_HAND); } catch (const BadConversion& er) { // Handle bad conversions cerr << "Conversion error: " << er.what() << endl << "\tretrieved data size: " << er.retrieved << ", actual size: " << er.actual_size << endl; wxMessageBox("Conversino Error!", "Conversin Error Detected", wxICON_HAND); } catch (const Exception& er) { // Catch-all for any other MySQL++ exceptions string buf = er.what(); cerr << "Error: " << er.what() << endl; wxMessageBox("Miscalleanous Error!", "Other Error Detected", wxICON_HAND); } } void zDB01::selDB() { dlgSelDB dlg(NULL,wxID_ANY); dlg.lbxDbNames->InsertItems(dbn, 0); if (dlg.ShowModal() == wxID_OK) { } }
[/codesyntax]
The header file follows.
[codesyntax lang=”cpp”]
#ifndef ZDB01_H #define ZDB01_H #include <mysql++.h> #include <wx/arrstr.h> class zDB01 { public: zDB01(); virtual ~zDB01(); void listDatabases(); wxArrayString dbn; void selDB(); wxString db; protected: private: }; #endif // ZDB01_H
[/codesyntax]
The dialog used in this example is as follows.
[codesyntax lang=”cpp”]
#include "dlgSelDB.h" //(*InternalHeaders(dlgSelDB) #include <wx/intl.h> #include <wx/string.h> //*) //(*IdInit(dlgSelDB) const long dlgSelDB::ID_LISTBOX1 = wxNewId(); //*) BEGIN_EVENT_TABLE(dlgSelDB,wxDialog) //(*EventTable(dlgSelDB) //*) END_EVENT_TABLE() dlgSelDB::dlgSelDB(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size) { //(*Initialize(dlgSelDB) wxBoxSizer* BoxSizer1; wxBoxSizer* BoxSizer2; wxBoxSizer* BoxSizer3; Create(parent, id, _("Select Database"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxSYSTEM_MENU|wxRESIZE_BORDER, _T("id")); SetClientSize(wxSize(-1,-1)); Move(wxDefaultPosition); SetMaxSize(wxSize(-1,-1)); BoxSizer1 = new wxBoxSizer(wxHORIZONTAL); BoxSizer2 = new wxBoxSizer(wxHORIZONTAL); BoxSizer3 = new wxBoxSizer(wxHORIZONTAL); lbxDbNames = new wxListBox(this, ID_LISTBOX1, wxDefaultPosition, wxSize(358,256), 0, 0, 0, wxDefaultValidator, _T("ID_LISTBOX1")); BoxSizer3->Add(lbxDbNames, 1, wxALL|wxEXPAND, 5); BoxSizer2->Add(BoxSizer3, 1, wxALL|wxEXPAND, 5); BoxSizer1->Add(BoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5); SetSizer(BoxSizer1); SetSizer(BoxSizer1); Layout(); Connect(ID_LISTBOX1,wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,(wxObjectEventFunction)&dlgSelDB::OnlbxDbNamesDClick); //*) } dlgSelDB::~dlgSelDB() { //(*Destroy(dlgSelDB) //*) } void dlgSelDB::OnlbxDbNamesDClick(wxCommandEvent& event) { buf = lbxDbNames->GetStringSelection().ToStdString(); EndModal(wxID_OK); }
[/codesyntax]
The header file is as follows.
[codesyntax lang=”cpp”]
#ifndef DLGSELDB_H #define DLGSELDB_H //(*Headers(dlgSelDB) #include <wx/dialog.h> #include <wx/listbox.h> #include <wx/sizer.h> //*) class dlgSelDB: public wxDialog { public: dlgSelDB(wxWindow* parent,wxWindowID id=wxID_ANY,const wxPoint& pos=wxDefaultPosition,const wxSize& size=wxDefaultSize); virtual ~dlgSelDB(); // wxString db; std::string buf; //(*Declarations(dlgSelDB) wxListBox* lbxDbNames; //*) protected: //(*Identifiers(dlgSelDB) static const long ID_LISTBOX1; //*) private: //(*Handlers(dlgSelDB) void OnlbxDbNamesDClick(wxCommandEvent& event); //*) DECLARE_EVENT_TABLE() }; #endif
[/codesyntax]
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.