How to insert wchar_t type string to PostgreSQL

Started by Yan Cheng Cheokalmost 16 years ago3 messagesgeneral
Jump to latest
#1Yan Cheng Cheok
yccheok@yahoo.com

Hello all,

I am programming in Windows environment. Previously, we are using MBCs (http://msdn.microsoft.com/en-us/library/5z097dxa%28VS.71%29.aspx), in order to support language other than English.

The following function still can be used, as long as we are setting the database to correct encoding. For example, if we want to support china mainland, we will be using "GBK" as encoding.

PGresult *PQexec(PGconn *conn, const char *query);

But once we compile using unicode, we are no longer using char * typed string. Instead, we are using wchar_t * typed string.

But, I do not find any function as follow in libpq library.

PGresult *PQexec(PGconn *conn, const wchar_t *query);

May I know, how I can store wchar_t * typed string into PostgreSQl, using C library?

Thanks and Regards
Yan Cheng CHEOK

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Yan Cheng Cheok (#1)
Re: How to insert wchar_t type string to PostgreSQL

Yan Cheng CHEOK <yccheok@yahoo.com> writes:

May I know, how I can store wchar_t * typed string into PostgreSQl, using C library?

You can't; you have to convert to whatever the client_encoding is.

regards, tom lane

#3John R Pierce
pierce@hogranch.com
In reply to: Yan Cheng Cheok (#1)
Re: How to insert wchar_t type string to PostgreSQL

Yan Cheng CHEOK wrote:

Hello all,

I am programming in Windows environment. Previously, we are using MBCs (http://msdn.microsoft.com/en-us/library/5z097dxa%28VS.71%29.aspx), in order to support language other than English.

The following function still can be used, as long as we are setting the database to correct encoding. For example, if we want to support china mainland, we will be using "GBK" as encoding.

PGresult *PQexec(PGconn *conn, const char *query);

But once we compile using unicode, we are no longer using char * typed string. Instead, we are using wchar_t * typed string.

But, I do not find any function as follow in libpq library.

PGresult *PQexec(PGconn *conn, const wchar_t *query);

May I know, how I can store wchar_t * typed string into PostgreSQl, using C library?

Microsoft's "Unicode" is UTF-16. The entire rest of the world uses
UTF-8. You have two choices.

A) convert your wchar_t to UTF8 for storage in SQL

B) store your wchar_t stuff as BYTEA (essentially blob).

option B has the disadvantage of not being able to do any string
manipulation in SQL.