将std :: wstring转换为SQLWCHAR * [英] Converting std::wstring to SQLWCHAR *

查看:737
本文介绍了将std :: wstring转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆