wxChoice add values from DB Topic is solved

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

wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 3:56 am

I am a newb with wxWidgets.
I have a database setup and i would like to fill a wxChoice with values from a select statement.

Choice1->Append(_("name1")) only seems to work with quotes around a hardcoded string

How can i use a string variable instead of hardcoding the values?

Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
string t1;
t1="test";
Choice1->Append(wxT("t1"));
/////////////object
dbcon Conn;
Conn.SetConnection();
pqxx::result R;
R=Conn.querry("select state from states");
Conn.Disconnect();
result::const_iterator d = R.begin();
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {

string r_state;
r_state = c["state"].as<string>();
string sr_state=r_state.str();
}

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

Re: wxChoice add values from DB

Post by doublemax » Sun Dec 08, 2013 10:42 am

Choice1->Append(_("name1")) only seems to work with quotes around a hardcoded string
This should also work string variables. If it doesn't work for you, please post the exact compiler error.

BTW: What platform and wxWidgets version?
Use the source, Luke!

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

Re: wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 1:52 pm

versions:
Codeblocks 10.5
wxsmith 1.0
wxsmithaui 0.8.1
wxsmithcontribitems .1
wxsmithmeme 1

code:
Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
string t1;
t1="test";
Choice1->Append(wxT("t1")); // this works
Choice1->Append(wxT(t1)); // this fails

compiler error:
||=== bdn, Debug ===|
/home/aj/bdn/editmasters_dialog.cpp||In constructor ‘editmasters_dialog::editmasters_dialog(wxWindow*, wxWindowID, const wxPoint&, const wxSize&)’:|
/home/aj/bdn/editmasters_dialog.cpp|47|error: ‘Lt1’ was not declared in this scope|
/home/aj/bdn/editmasters_dialog.cpp|62|error: no matching function for call to ‘wxString::wxString(std::string&)’|
/home/aj/bdn/editmasters_dialog.cpp|62|note: candidates are:|
/usr/include/wx-2.8/wx/string.h|1283|note: wxString::wxString(const void*, const void*)|
/usr/include/wx-2.8/wx/string.h|1283|note: candidate expects 2 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|1280|note: wxString::wxString(const wxString&, size_t, size_t)|
/usr/include/wx-2.8/wx/string.h|1280|note: candidate expects 3 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|722|note: wxString::wxString(const wxWCharBuffer&)|
/usr/include/wx-2.8/wx/string.h|722|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxWCharBuffer&’|
/usr/include/wx-2.8/wx/string.h|720|note: wxString::wxString(const char*, const wxMBConv&, size_t)|
/usr/include/wx-2.8/wx/string.h|720|note: candidate expects 3 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|714|note: wxString::wxString(const wxStdString&)|
/usr/include/wx-2.8/wx/string.h|714|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxStdString& {aka const std::basic_string<wchar_t>&}’|
/usr/include/wx-2.8/wx/string.h|702|note: wxString::wxString(const wxChar*, const wxMBConv&, size_t)|
/usr/include/wx-2.8/wx/string.h|702|note: candidate expects 3 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|700|note: wxString::wxString(const wxChar*, size_t)|
/usr/include/wx-2.8/wx/string.h|700|note: candidate expects 2 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|698|note: wxString::wxString(const wxChar*)|
/usr/include/wx-2.8/wx/string.h|698|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxChar* {aka const wchar_t*}’|
/usr/include/wx-2.8/wx/string.h|694|note: wxString::wxString(size_t, wxChar)|
/usr/include/wx-2.8/wx/string.h|694|note: candidate expects 2 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|692|note: wxString::wxString(wxChar, size_t)|
/usr/include/wx-2.8/wx/string.h|692|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘wxChar {aka wchar_t}’|
/usr/include/wx-2.8/wx/string.h|690|note: wxString::wxString(const wxString&)|
/usr/include/wx-2.8/wx/string.h|690|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxString&’|
/usr/include/wx-2.8/wx/string.h|689|note: wxString::wxString(const wxStringBase&)|
/usr/include/wx-2.8/wx/string.h|689|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxStringBase&’|
/usr/include/wx-2.8/wx/string.h|687|note: wxString::wxString()|
/usr/include/wx-2.8/wx/string.h|687|note: candidate expects 0 arguments, 1 provided|
/usr/include/wx-2.8/wx/string.h|682|note: wxString::wxString(int)|
/usr/include/wx-2.8/wx/string.h|682|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘int’|
||=== Build finished: 31 errors, 0 warnings ===|

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

Re: wxChoice add values from DB

Post by doublemax » Sun Dec 08, 2013 2:11 pm

The wxT() macro is for string literals only, not variables.

This should work:

Code: Select all

Choice1->Append( t1 );
Use the source, Luke!

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

Re: wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 2:14 pm

Thanks for the quick responses!
I just tried this:

Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
string t1;
t1="test";
Choice1->Append( t1 );

compiler error:
/home/aj/bdn/editmasters_dialog.cpp||In constructor ‘editmasters_dialog::editmasters_dialog(wxWindow*, wxWindowID, const wxPoint&, const wxSize&)’:|
/home/aj/bdn/editmasters_dialog.cpp|47|error: no matching function for call to ‘wxChoice::Append(std::string&)’|
/home/aj/bdn/editmasters_dialog.cpp|47|note: candidates are:|
/usr/include/wx-2.8/wx/ctrlsub.h|98|note: int wxItemContainer::Append(const wxString&)|
/usr/include/wx-2.8/wx/ctrlsub.h|98|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxString&’|
/usr/include/wx-2.8/wx/ctrlsub.h|100|note: int wxItemContainer::Append(const wxString&, void*)|
/usr/include/wx-2.8/wx/ctrlsub.h|100|note: candidate expects 2 arguments, 1 provided|
/usr/include/wx-2.8/wx/ctrlsub.h|102|note: int wxItemContainer::Append(const wxString&, wxClientData*)|
/usr/include/wx-2.8/wx/ctrlsub.h|102|note: candidate expects 2 arguments, 1 provided|
/usr/include/wx-2.8/wx/ctrlsub.h|110|note: void wxItemContainer::Append(const wxArrayString&)|
/usr/include/wx-2.8/wx/ctrlsub.h|110|note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const wxArrayString&’|
||=== Build finished: 10 errors, 0 warnings ===|

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

Re: wxChoice add values from DB

Post by doublemax » Sun Dec 08, 2013 2:21 pm

You really should use wxWidgets 3.0.

In the "wx world", it's easier to work with wxString instead of std::string. If you want to use STL string, at least use wstring, otherwise you will have to deal with 8bit encoding conversions.

Code: Select all

Choice1->Append( wxString(t1, wxConvUTF8) );
or
Choice1->Append( wxString(t1, wxConvLocal) );
Depending on the 8bit encoding used.
Use the source, Luke!

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

Re: wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 2:34 pm

thanks for the help.
I had installed it form the ubuntu software center.
Where is version 3.0?

I really appreciate all the help.

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

Re: wxChoice add values from DB

Post by doublemax » Sun Dec 08, 2013 2:56 pm

wx 3.0 is relatively new, so it's probably not available yet through the software center.

You probably have to build it yourself from the source:
http://www.wxwidgets.org/downloads/

Or continue with 2.8.12 for now.
Use the source, Luke!

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

Re: wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 2:57 pm

I did get the code to compile using the following char* data type:

Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
char* t1;
t1="test";

Choice1->Append( wxString(t1, wxConvLocal) );

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

Re: wxChoice add values from DB

Post by guzzi_jones » Sun Dec 08, 2013 3:08 pm

And this worked to grab the values from a postgresql database:
I hope this helps someone in the future. I am sure learning a lot.


/// connection header file:
#ifndef DBCON_H
#define DBCON_H

#include <pqxx/pqxx>
#include <iostream>
#include <string>


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=aj password= hostaddr=127.0.0.1 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=trans.exec(strSQL);

trans.commit();
return res;
}
void Disconnect(){

C->disconnect ();
// delete C;


}


};

//////////end connection header file


///////////// .cpp file
Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
char* t1;
t1="test";

Choice1->Append( wxString(t1, wxConvLocal) );

dbcon Conn;
Conn.SetConnection();
pqxx::result R;
R=Conn.querry("select state from states");
Conn.Disconnect();
result::const_iterator d = R.begin();
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {

// stringstream r_state;
const char* r_state;
r_state = c["state"].c_str();

Choice1->Append( wxString(r_state, wxConvLocal) );

}

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

Re: wxChoice add values from DB

Post by doublemax » Sun Dec 08, 2013 3:15 pm

Code: Select all

Choice1->Append( wxString(r_state, wxConvLocal) );
Even is this works, it's not necessarily the right thing to do. Like i mentioned before, if you deal with 8-bit data, it's important to know its encoding. If the data comes from a database, it's more likely to be encoded in UTF-8. You have to research which encoding the database uses.
Use the source, Luke!

Post Reply