ODBC 和 NLS_LANG

发布于 2024-09-06 07:29:50 字数 1020 浏览 3 评论 0原文

假设我已经创建了两个不同的可执行程序,例如用C++。

由于某种原因,两个程序内部的文本表示方式彼此不同。假设第一个程序使用文本表示形式 A 和另一个文本表示形式 B。它可以是特定的 8 位 ANSI 代码页、Unicode/UTF-8 或 Unicode/UTF-16 或其他。

现在,每个程序都希望与(数据库)服务器上的同一个数据库表进行文本通信(添加/检索数据)。每个程序都通过ODBC与数据库进行通信。因此程序不知道它们正在与哪个数据库系统进行通信。

在这种特定情况下,尽管数据库实际上是 Oracle RDMS 数据库,并且数据库服务器管理员已将数据库设置为使用 UTF-8。

在运行程序的系统上有适当的 ODBC 驱动程序可用,以便程序可以通过 ODBC 连接。每个程序都会适当地处理 ODBC 数据类型 SQL_C_CHAR 并将其转换为其内部文本表示形式。我假设程序除了假设为 SQL_C_CHAR 文本返回特定编码之外别无选择。如果不是,则必须告诉程序那是哪种编码。

对于 Oracle,我知道 NLS_LANG 环境变量可以在客户端使用。我假设它会影响 ODBC 驱动程序(与 SQL_C_CHAR 相关)从特定编码(由 NLS_LANG 给出)转换为数据库的内部编码(在本例中为 UTF-8),反之亦然。

如果运行我的程序的计算机具有 NLS_LANG,此设置将影响为 SQL_C_CHAR 返回的字节序列,因此我的程序无法突然为通过 SQL_C_CHAR 返回的文本采用特定编码。

是否可以设置 ODBC 连接(最好在运行时以编程方式),以便它适当地处理两个程序的文本转换,即从/到表示形式到/从 UTF-8 以及从/到表示形式 B 到/从 UTF -8?

问候, /迈克尔

PS。由于程序通过 ODBC 连接,我认为它们现在应该了解有关 NLS_LANG 的任何信息,这不会很好,因为这是 Orcacle 特定的环境变量。

Let's say that I've created two different program executables, e.g. in C++.

For some reason, the two programs internals representation of text are different from each other. Let's say the first program is using text representation A and the other text representation B. It could be a specific 8-bit ANSI codepage, Unicode/UTF-8 or Unicode/UTF-16 or whatever.

Now each program want to communicate text (add/retrieve data) to/from the same database table on a (database) server. Each program communicates with the database through ODBC. So the programs do not know what database system they they are communicating with.

In this specific case though the database is actually a Oracle RDMS database and the database server administrator has setup the database to use UTF-8.

On the system on which the programs are running an appropriate ODBC driver is available, so that the programs can connect through ODBC. Each program will treat and convert from the ODBC data type SQL_C_CHAR to its internal text representation appropriately. I assume that the programs cannot do no other than to assume a specific encoding returned for SQL_C_CHAR text. If not the programs has to be told which encoding that is.

For Oracle, I know that the NLS_LANG environment variable can be used on the client. I assume it affects the ODBC driver (related to SQL_C_CHAR) to convert from a specific encoding (as given by NLS_LANG) to the internal encoding of the database (in this example UTF-8) and vice-versa.

If the machine running my programs are having a NLS_LANG this setting will affect the byte sequences returned for SQL_C_CHAR so my programs cannot suddenly assume a specific encoding for the text returned via SQL_C_CHAR.

Is it possible to setup the ODBC connection (preferably programmatically at runtime), so that it takes care of text conversions appropriately for the two programs, i.e. from/to representation to/from UTF-8 and from/to representation B to/from UTF-8?

Regards,
/Michael

PS. As the programs are connecting through ODBC I don't think it would be nice that they should now anything about NLS_LANG as this is a Orcacle specific environment variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-09-13 07:29:50

您需要使用定义的 UNICODE 宏来构建应用程序(请参阅 sqltypes.h 和 sqlucode.h 等 sql 头文件)。这将对通常映射到 SQLxxxA (ANSI) 的 SQLxxx 的调用更改为 SQLxxxW,因此它们使用所谓的宽字符 API。这意味着您可以将 unicode(以及 UCS2 编码数据)传递给 SQL API,并结合以 SQL_WCHAR 形式获取字符串列,您将从获取中获取宽数据。我可能错过了它,但我没有看到您提到平台(Windows 或 UNIX),如果您使用的是 unix 并且没有使用 unixODBC 驱动程序管理器,那么这可能会产生小小的差异。 MS 站点上有有关 unicode 和 ODBC 的负载。

使用 Easysoft ODBC-Oracle 对 Oracle/Unicode/ODBC 有合理的解释带有 Unicode 数据的驱动程序。它解释了 C 上下文中关于 Oracle/NLS_LANG 的 ODBC 和 Unicode。

You need to build your app with the UNICODE macro defined (see sql header files like sqltypes.h and sqlucode.h). This changes the calls to SQLxxx which normally map to SQLxxxA (ANSI) to SQLxxxW and so they are using the so called Wide Character APIs. This means you can pass unicode (well UCS2 encoded data) to SQL APIs and combined with fetching string columns as SQL_WCHAR you'll get wide data from fetches. I may have missed it but I don't see you mention a platform (Windows or UNIX) and that may make a small difference if for instance you are on unix and are not using the unixODBC Driver Manager. There is loads on the MS site about unicode and ODBC.

There is a reasonable explanation of Oracle/Unicode/ODBC at Using the Easysoft ODBC-Oracle Driver with Unicode Data. It explains ODBC and Unicode wrt Oracle/NLS_LANG in a C context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文