Re: wxWidgets and PostgreSQL

100 views
Skip to first unread message

Vadim Zeitlin

unread,
Nov 2, 2007, 9:26:03 AM11/2/07
to wx-u...@lists.wxwidgets.org
On Fri, 2 Nov 2007 20:12:16 +0700 Ayen Yang <ayen....@gmail.com> wrote:

AY> I think the problem is in the conversion from the PQgetvalue() to
AY> wxString, but I don't know for sure.
AY> I've tried many of wxString conversion functions, but the result is
AY> still the same.

You don't need to try many different things, you'll just confuse yourself
and anybody else trying to help you. You need to know 2 simple things: what
is the string format returned by PQgetvalue() and what build of wx do you
use. If you know this, wxString provides the conversion function you need.
If you don't, you risk to waste a lot of time stumbling in the dark.

This is (by far) not the first time this question comes on this list and
I'm amazed that people think that you need some black magic to convert
strings between different encodings and Unicode. There is really nothing
complicated in it if you just stop a moment and try to understand what
you're doing. In particular, if you're guessing about what to do, something
is wrong. You need to know, and not to guess. So while I don't know what
does PQgetvalue() return, you really should -- but it seems that you don't.
And until you know this it's useless to try to do anything.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/


Vadim Zeitlin

unread,
Nov 2, 2007, 9:52:51 AM11/2/07
to wx-u...@lists.wxwidgets.org
On Fri, 2 Nov 2007 20:48:09 +0700 Ayen Yang <ayen....@gmail.com> wrote:

AY> The PQgetvalue() function returns char*

This is not enough. A non-ASCII "char *" string can be encoded either
using UTF-8 or any of single byte (e.g. ISO-8859-X) encodings. Of course,
if your string contains only ASCII characters it doesn't matter as all
these encodings encode 7 bit ASCII characters as themselves so you can just
use wxString::FromAscii(). But if you want it to work with any strings you
must know what encoding is used and use the corresponding conversion object
in the wxString ctor (assuming you use the Unicode build, you shouldn't be
trying to handle strings in arbitrary encodings in ANSI build as while it
is possible, it's much more complicated).

Ayen Yang

unread,
Nov 2, 2007, 9:12:16 AM11/2/07
to wx-u...@lists.wxwidgets.org
Hello Everyone,

Since the ODBC class will be discontinued, I tried to use the native
library of PostgreSQL to access the database.

I tried to get some data from a table and put them in a ListCtrl. I
found a problem that the character displayed in the ListCtrl somehow
'scrambled' and cannot be read (some of the characters look like
Chinese and Korean).


I think the problem is in the conversion from the PQgetvalue() to

wxString, but I don't know for sure.

I've tried many of wxString conversion functions, but the result is

still the same.

Does anyone ever experience the same problem? Please help me.

Thanks in advance,
Ayen

Vadim Zeitlin

unread,
Nov 2, 2007, 11:58:12 AM11/2/07
to wx-u...@lists.wxwidgets.org
On Fri, 2 Nov 2007 21:46:09 +0700 Ayen Yang <ayen....@gmail.com> wrote:

AY> I've just tried to use wxString::FromAscii() and the result is still
AY> the same.

What exactly does PQgetvalue() return to you?

AY> I also tried wxString::FromUTF8() and the result is an empty wxString.

This can only happen if the string contains 8 bit characters and is not
encoded in UTF-8.

AY>
AY> I'm a newbie in wxWidgets programming. Could you tell me how to put
AY> the value to a wxString:
AY> - if the PQgetvalue() returns UTF8?

wxString::FromUTF8().

AY> - if the PQgetvalue() returns ASCII?

wxString::FromAscii().

Ayen Yang

unread,
Nov 2, 2007, 9:48:09 AM11/2/07
to wx-u...@lists.wxwidgets.org
Dear VZ,

Thanks for your reply.

The PQgetvalue() function returns char*

I've tried to insert the returned value to a wxString variable, as below:

wxString c;
c.Printf( wxT("%s"), PQgetvalue(result, 0, 0) );

note: PQgetvalue(result, 0, 0) is a PostgreSQL function to retrieve
the value of the first column in the first row of 'result'.

The PQgetvalue should return an English statement as on the table, but
the resulting c contains different characters.
Please help me where I made mistakes. Thank you very very much.

Regards,
Ayen

> Regards,
> VZ
>
> --
> TT-Solutions: wxWidgets consultancy and technical support
> http://www.tt-solutions.com/
>
>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wx-users-u...@lists.wxwidgets.org
> For additional commands, e-mail: wx-use...@lists.wxwidgets.org
>
>

Ayen Yang

unread,
Nov 2, 2007, 10:46:09 AM11/2/07
to wx-u...@lists.wxwidgets.org
Dear VZ,

I've just tried to use wxString::FromAscii() and the result is still
the same. I also tried wxString::FromUTF8() and the result is an empty
wxString.

I'm a newbie in wxWidgets programming. Could you tell me how to put


the value to a wxString:

- if the PQgetvalue() returns UTF8?

- if the PQgetvalue() returns ASCII?

Thank you very much.

Regards,
Ayen

On Nov 2, 2007 8:52 PM, Vadim Zeitlin <va...@wxwidgets.org> wrote:
> On Fri, 2 Nov 2007 20:48:09 +0700 Ayen Yang <ayen....@gmail.com> wrote:
>
> AY> The PQgetvalue() function returns char*
>
> This is not enough. A non-ASCII "char *" string can be encoded either
> using UTF-8 or any of single byte (e.g. ISO-8859-X) encodings. Of course,
> if your string contains only ASCII characters it doesn't matter as all
> these encodings encode 7 bit ASCII characters as themselves so you can just
> use wxString::FromAscii(). But if you want it to work with any strings you
> must know what encoding is used and use the corresponding conversion object
> in the wxString ctor (assuming you use the Unicode build, you shouldn't be
> trying to handle strings in arbitrary encodings in ANSI build as while it
> is possible, it's much more complicated).
>
>

Ayen Yang

unread,
Nov 2, 2007, 10:47:15 AM11/2/07
to wx-u...@lists.wxwidgets.org
By the way, the database is encoded in UTF8 and I'm compiling the Unicode built.
Thanks.

Ayen Yang

unread,
Nov 2, 2007, 11:41:01 AM11/2/07
to wx-u...@lists.wxwidgets.org
Dear VZ,

I just tried to build the ANSI version of my wxWidgets application and
the problems are gone.
Could you tell me what is wrong?

Thank you very very much.

Regards,
Ayen

三无人员

unread,
Nov 4, 2007, 9:25:54 PM11/4/07
to wx-u...@lists.wxwidgets.org
Maybe this is a bug of wxWidgets.
I build unicode/DLL version of wxWidgets, but when I use wxString str_u( pStr, wxConvUTF8 ), I failed.
But when I use std::string str( pStr ), wxString str_u( str.c_str(), wxConvUTF8 ),
it works correctly.

--
MUOP_WS : a cross-platform co-operating software like Lotus-Notes

http://sourceforge.net/projects/muop-ws
http://soaringhawkzf.googlepages.com

Recommend for using TOR to explorer Internet

在2007-11-02,"Vadim Zeitlin" <va...@wxwidgets.org> 写道:
On Fri, 2 Nov 2007 21:46:09 +0700 Ayen Yang <ayen....@gmail.com
> wrote:

AY> I've just tried to use wxString::FromAscii() and the result is still
AY> the same.

 What exactly does PQgetvalue() return to you?

AY> I also tried wxString::FromUTF8() and the result is an empty wxString.

 This can only happen if the string contains 8 bit characters and is not
encoded in UTF-8.

AY> 
AY> I'm a newbie in wxWidgets programming. Could you tell me how to put
AY> the value to a wxString:
AY> - if the PQgetvalue() returns UTF8?

 wxString::FromUTF8().

AY> - if the PQgetvalue() returns ASCII?

 wxString::FromAscii().

 Regards,
VZ

-- 
TT-Solutions: wxWidgets consultancy and technical support
               http://www.tt-solutions.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-u...@lists.wxwidgets.org
For additional commands, e-mail: wx-use...@lists.wxwidgets.org




LOGO 把爱心注入牛奶,共同凝聚这份力量
快来参加蒙牛免费赠奶爱心行动

Vadim Zeitlin

unread,
Nov 5, 2007, 8:24:24 AM11/5/07
to wx-u...@lists.wxwidgets.org
On Mon, 5 Nov 2007 10:25:54 +0800 (CST) ÈýÎÞÈËÔ± <zj...@163.com> wrote:

È> I build unicode/DLL version of wxWidgets, but when I use wxString str_u( pStr, wxConvUTF8 ), I failed.
È> But when I use std::string str( pStr ), wxString str_u( str.c_str(), wxConvUTF8 ),
È> it works correctly.

Sorry, this is impossible. You call the same ctor with the same arguments
so it must work in the same way. Or your "pStr" pointer is invalid in the
first case.

Reply all
Reply to author
Forward
0 new messages