将std :: wstring转换为SQLWCHAR * [英] Converting std::wstring to SQLWCHAR *
问题描述
我有一个C ++程序,动态创建一个查询字符串,然后传递到使用ODBC连接到数据库的 SQLExecDirect
调用。
I have a C++ program that is dynamically creating a query string which will then be passed to a SQLExecDirect
call to connect to a database using ODBC. I'm having trouble passing the variable from one function to another, so I think I must be missing something basic?
在 ConstructQuery
In the ConstructQuery
function (which returns type SQLWCHAR *
), I have:
std::wstring test = L"test string"; //This test string will actually be several concatenated strings
SQLWCHAR *statement = (SQLWCHAR *)test.c_str();
std::wcout << statement;
return statement;
这样会按预期打印语句变量。但是当我将变量传递给我的主要函数,像这样:
This prints the statement variable as expected. But when I pass the variable to my main function like this:
SQLStatement = ConstructQuery(SQLStatement);
std::wcout << SQLStatement;
我没有输出。
如果不是 statement =(SQLWCHAR *)test.c_str()
;
我使用: statement = Ltest string
;
通过罚款,但是我不能在函数的早期部分动态创建测试字符串查询。
The variable passes fine, but then I am not able to dynamically create the "test string" query in the earlier part of the function.
我很难找到很多约 SQLWCHAR
。我猜我可能会转换 std :: wstring
到 SQLWCHAR *
不正确?另一种选择是重写函数,使所有 wstring
都是 SQLWCHAR *
,并以这种方式进行连接 -
I was having a hard time finding out much about SQLWCHAR
. I'm guessing that I may be converting std::wstring
to SQLWCHAR *
incorrectly? Another option would be to rewrite the function so that all of the wstring
are SQLWCHAR *
and do the concatenation that way - but I'm not sure that's possible and even if it was I don't think it's preferred?
推荐答案
您返回的是一个指向在函数ConstructQuery结束时超出作用域的局部变量的指针。可能最容易通过值返回 std :: wstring
,然后从那里开始工作。
You are returning a pointer to a local variable that goes out of scope at the end of the function ConstructQuery. It might be easiest to return a std::wstring
by value and then work from there.
这篇关于将std :: wstring转换为SQLWCHAR *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!