主要源代码如下:
/***************************************************************
* Name: MyGodMain.cpp
* Purpose: Code for example to access the Postgresql system.
* Author: Yavobo
* Created: 2010-02-06
* Copyright: Yavobo
* License: LGPL
**************************************************************/
#include "MyGodMain.h"
#include <wx/msgdlg.h>
#include <libpq-fe.h>
//(*InternalHeaders(MyGodDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//helper functions
enum wxbuildinfoformat
{
short_f,long_f
};
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
//(*IdInit(MyGodDialog)
const long MyGodDialog::ID_STATICTEXT1 = wxNewId();
const long MyGodDialog::ID_TEXTCTRL1 = wxNewId();
const long MyGodDialog::ID_BUTTON1 = wxNewId();
const long MyGodDialog::ID_BUTTON3 = wxNewId();
const long MyGodDialog::ID_BUTTON2 = wxNewId();
const long MyGodDialog::ID_GRID1 = wxNewId();
//*)
//==================================================================
// 这个是http://hi.baidu.com/nivrrex/blog/item/a82febc4614955ae8226ac2e.html中的代码
wchar_t * ANSIToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_ACP,str,-1,NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP,(LPWSTR)result,textlen );
return result;
}
char * UnicodeToANSI( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_ACP,NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset( result,sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_ACP,result,textlen,NULL );
return result;
}
wchar_t * UTF8ToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_UTF8,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,textlen );
return result;
}
char * UnicodeToUTF8( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_UTF8,NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset(result,sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_UTF8,NULL );
return result;
}
/*宽字符转换为多字符Unicode - ANSI*/
char* w2m(const wchar_t* wcs)
{
int len;
char* buf;
len =wcstombs(NULL,wcs,0);
if (len == 0)
return NULL;
buf = (char *)malloc(sizeof(char)*(len+1));
memset(buf,sizeof(char) *(len+1));
len =wcstombs(buf,len+1);
return buf;
}
/*多字符转换为宽字符ANSI - Unicode*/
wchar_t* m2w(const char* mbs)
{
int len;
wchar_t* buf;
len =mbstowcs(NULL,mbs,0);
if (len == 0)
return NULL;
buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
memset(buf,sizeof(wchar_t) *(len+1));
len =mbstowcs(buf,len+1);
return buf;
}
char* ANSIToUTF8(const char* str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
char* UTF8ToANSI(const char* str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
//由nivrrex先生编写。在此予以感谢!
//==================================================================
BEGIN_EVENT_TABLE(MyGodDialog,wxDialog)
//(*EventTable(MyGodDialog)
//*)
END_EVENT_TABLE()
MyGodDialog::MyGodDialog(wxWindow* parent,wxWindowID id)
{
//(*Initialize(MyGodDialog)
wxFlexGridSizer* FlexGridSizer4;
wxFlexGridSizer* FlexGridSizer5;
wxFlexGridSizer* FlexGridSizer6;
Create(parent,wxID_ANY,_("wxWidgets app"),wxDefaultPosition,wxDefaultSize,wxDEFAULT_DIALOG_STYLE,_T("wxID_ANY"));
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
FlexGridSizer4 = new wxFlexGridSizer(0,3,0);
StaticText1 = new wxStaticText(this,ID_STATICTEXT1,_("姓名:"),_T("ID_STATICTEXT1"));
FlexGridSizer4->Add(StaticText1,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
TextCtrl1 = new wxTextCtrl(this,ID_TEXTCTRL1,_("Text"),wxSize(242,22),wxDefaultValidator,_T("ID_TEXTCTRL1"));
FlexGridSizer4->Add(TextCtrl1,5);
BoxSizer2->Add(FlexGridSizer4,5);
FlexGridSizer6 = new wxFlexGridSizer(0,0);
Button1 = new wxButton(this,ID_BUTTON1,_("关于"),_T("ID_BUTTON1"));
FlexGridSizer6->Add(Button1,4);
Button3 = new wxButton(this,ID_BUTTON3,_("你是@¥*12)+!#"),_T("ID_BUTTON3"));
FlexGridSizer6->Add(Button3,5);
Button2 = new wxButton(this,ID_BUTTON2,_("退出"),_T("ID_BUTTON2"));
FlexGridSizer6->Add(Button2,4);
BoxSizer2->Add(FlexGridSizer6,5);
FlexGridSizer5 = new wxFlexGridSizer(0,0);
Grid1 = new wxGrid(this,ID_GRID1,wxSize(450,200),wxSTATIC_BORDER,_T("ID_GRID1"));
Grid1->CreateGrid(500,6);
Grid1->EnableEditing(true);
Grid1->EnableGridLines(true);
Grid1->SetRowLabelSize(30);
Grid1->SetDefaultCellFont( Grid1->GetFont() );
Grid1->SetDefaultCellTextColour( Grid1->GetForegroundColour() );
FlexGridSizer5->Add(Grid1,5);
BoxSizer2->Add(FlexGridSizer5,5);
BoxSizer1->Add(BoxSizer2,wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,4);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this);
BoxSizer1->SetSizeHints(this);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyGodDialog::OnAbout);
Connect(ID_BUTTON3,(wxObjectEventFunction)&MyGodDialog::OnButton3Click);
Connect(ID_BUTTON2,(wxObjectEventFunction)&MyGodDialog::OnQuit);
//*)
}
MyGodDialog::~MyGodDialog()
{
//(*Destroy(MyGodDialog)
//*)
}
void MyGodDialog::OnQuit(wxCommandEvent& event)
{
Close();
}
void MyGodDialog::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
msg.Printf(_T("这是一个参数%d!"),150);
wxMessageBox(msg,_("Welcome to..."));
}
void MyGodDialog::OnButton3Click(wxCommandEvent& event)
{
PGconn *conn;
PGresult *res;
int nFields;
int i,j;
wxString str;
conn = PQconnectdb("host=localhost dbname=test user=admin password=123456 port=5432 connect_timeout=30");
if(PQstatus(conn) != CONNECTION_OK)
{
wxMessageBox(_T("与Postgresql数据库系统连接失败."),_T("警告"));
PQfinish(conn);
}
PQsetClientEncoding(conn,"UNICODE");
res = PQexec(conn,"BEGIN");
//PQexec(conn,"INSERT INTO weather(id,city,temp_lo,temp_hi,prcp,date) VALUES (8,'高州',34,0.44,'2003-1-9');");
PQclear(res);
res = PQexec(conn,("DECLARE mycursor CURSOR FOR SELECT * FROM weather"));
PQclear(res);
res = PQexec(conn,("FETCH ALL in mycursor"));
nFields = PQnfields(res);
//PQexec(conn,("SET CLIENT_ENCODING TO 'GBK';"));
for (i = 0; i < nFields; i++)
{
str.Printf(_T("%s"),m2w(PQfname(res,i)));
Grid1->SetColLabelValue(i,str);
}
for (i = 0; i < PQntuples(res); i++)
{
for(j = 0; j < nFields; j++)
{
str.sprintf(wxT("%s"),(m2w(PQgetvalue(res,i,j))));
str = wxString(str.To8BitData(),wxConvUTF8);
//error:str.FromUTF8(m2w(PQgetvalue(res,j)));
//str.Printf(_("测试=%d"),i*j);
Grid1->SetCellValue(i,j,str);
if(i==PQntuples(res)-1 && j==1)TextCtrl1->SetValue(str);
}
}
PQclear(res);
PQexec(conn,"END");
res = PQexec(conn,"CLOSE mycursor");
PQclear(res);
PQfinish(conn);
}
需注意的一点是:用PQgetvalue返回的是ANSI编码的数据,而wxWidgets我用的是UTF-8,因此需将这两个编码加以互换。这里我用了nivrrex的几个转换函数(网址是:http://hi.baidu.com/nivrrex/blog/item/a82febc4614955ae8226ac2e.html)。完整的CODE::BLOCKS项目见我的下载资源栏目。
数据表结构:
CREATE TABLE weather(
id int,city varchar(80),temp_lo int,temp_hi int,prcp real,date date);