用wxWidgets编写访问PostgreSQL的示例

用wxWidgets编写访问PostgreSQL的示例,第1张

概述主要源代码如下: /***************************************************************  * Name:  MyGodMain.cpp  * Purpose: Code for example to access the PostgreSQL system.  * Author: Yavobo  * Created: 2010-02-06

主要源代码如下:

/***************************************************************
* @R_404_6889@: 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 db@R_404_6889@=test user=admin password=123456 port=5432 connect_timeout=30");
if(PQstatus(conn) != CONNECTION_OK)
{
wxMessageBox(_T("与Postgresql@R_419_6948@系统连接失败."),_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(PQf@R_404_6889@(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);

总结

以上是内存溢出为你收集整理的用wxWidgets编写访问PostgreSQL的示例全部内容,希望文章能够帮你解决用wxWidgets编写访问PostgreSQL的示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1180284.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存