Select 语句和隐式转换只给出字符串的第一个字符

发布于 2024-12-26 10:30:18 字数 529 浏览 1 评论 0原文

我正在 VC++ 中使用 OLEDB 连接到本地 Oracle 11gR2 数据库。我正在使用 CCommand::Open 从数据库中选择行,其中应包含字符串。

当我使用 GetValue 获取数据时,我只获取第一个字符。 这是我获取这些数据的尝试。请注意,“GetValue”和“GetColumnName”中也会发生相同的行为。

char* test = (CHAR*)cmd.GetColumnName(2);
cout << (CHAR*)cmd.GetColumnName(2) << endl;
printf_s( "%s", (CHAR*)cmd.GetColumnName(2));
printf_s( "%S", (CHAR*)cmd.GetColumnName(2)); //This one works, 
       //but I really need to store my data, not just print it.

我认为这是从 SQL 到 C++ 数据类型的转换问题,但我无法指出它。帮助?

I'm using OLEDB to connect to my local Oracle 11gR2 database in VC++. I'm using CCommand::Open to Select rows from my database, which should contain strings.

When I'm using GetValue to get my data though, I only get the first character.
Here are my attempts at getting that data. Note that the same behavior happens in "GetValue" and in "GetColumnName".

char* test = (CHAR*)cmd.GetColumnName(2);
cout << (CHAR*)cmd.GetColumnName(2) << endl;
printf_s( "%s", (CHAR*)cmd.GetColumnName(2));
printf_s( "%S", (CHAR*)cmd.GetColumnName(2)); //This one works, 
       //but I really need to store my data, not just print it.

I'm thinking this is a conversion problem from SQL to C++ data types, but I can't put my finger on it. Help?

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

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

发布评论

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

评论(1

感情旳空白 2025-01-02 10:30:18

LPOLESTR 是一个 wchar_t* 字符串(感谢 LRiO 确认),它基本上是 unsigned Short*。你只得到第一个字符的原因是因为每个字符占用两个字节,而英文字母恰好是一个 NULL 字节,后跟该字母的 ASCII 代码,当以小端格式存储时,它会成为一个 NULL 字节。具有一个字符的 C 字符串(因为字节向后存储)。

您需要使用 wcout 来打印它:

wcout << cmd.GetColumnName(2);

您可以像这样存储它(以及通过 wcslen 的长度):

LPOLESTR ostr = cmd.GetColumnName(2);
size_t ostrlen = wcslen(ostr);

LPOLESTR is a wchar_t* string (thanks LRiO for confirming that) which is basically unsigned short*. The reason you are getting just the first character is because each character takes up two bytes, and english letters happen to be a NULL byte followed by the ASCII code for that letter, which, when stored in a little-endian format, makes it a C-string with one character (because the bytes are stored backwards).

You need to use wcout to print it:

wcout << cmd.GetColumnName(2);

You can store it like this (along with its length via wcslen):

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