Select 语句和隐式转换只给出字符串的第一个字符
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LPOLESTR
是一个wchar_t*
字符串(感谢 LRiO 确认),它基本上是unsigned Short*
。你只得到第一个字符的原因是因为每个字符占用两个字节,而英文字母恰好是一个 NULL 字节,后跟该字母的 ASCII 代码,当以小端格式存储时,它会成为一个 NULL 字节。具有一个字符的 C 字符串(因为字节向后存储)。您需要使用
wcout
来打印它:您可以像这样存储它(以及通过
wcslen
的长度):LPOLESTR
is awchar_t*
string (thanks LRiO for confirming that) which is basicallyunsigned 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:You can store it like this (along with its length via
wcslen
):