Convert from std:string to SQLCHAR *

Hi.

I am looking for the correct conversion to SQLCHAR*

I am given several parameters as std:string. With this parameters I am glueing a correct conStatement together:

 
  string parameterString = "DRIVER={SQL Server};SERVER=" + server + ";DATABASE=master;UID=" + user + ";PWD=" + password;


But the function "SQLDriverConnect" does need a SQLCHAR *

I have tried nerarly everything (I know: the error sits behind the keyborad...) like:

 
  SQLCHAR * conString = reinterpret_cast<SQLCHAR*>(parameterString.c_str());


I have treid strcpy and append and... and...

I don't get it.

How to do it the right way?

Thanks for Help,
Taggi
It looks like SQLCHAR is unsigned char, so try
parameterString.c_str(), or
const_cast<SQLCHAR *>(parameterString.c_str());
Unfortunatelly it was not quite enough.

But you helped me much!

It has to be:

SQLCHAR* conString = (SQLCHAR*)(const_cast<char*>(parameterString.c_str()));

Not "beatyfull", but it works...

Thank you!

Taggi
c_str is already a char*, so that part is moot and can be pulled
you mixed c and c++ casts, a little odd, but removal of the char* part will only have one cast (the C style one..). I have no problem with c-style casts because the c++ ones are clunky, but most pros would say to prefer the c++ casting.
c_str is already a char*
Actually, it's const char * so the const_cast may be necessary to go const char * -> char * ->unsigned char *. To avoid the complication, I think I'd just do
(SQLCHAR *)parameterString.c_str()
According to
https://code.woboq.org/qt5/include/sqltypes.h.html

1
2
3
4
/****************************
 * standard SQL* data types. use these as much as possible when using ODBC calls/vars
 ***************************/
typedef unsigned char   SQLCHAR;
I'd suggest wrapping this functionality in an overloaded function.
This helps ensure you only const_cast pointers to data that are actually mutable.

1
2
3
SQLCHAR* as_sqlchar_str(std::string& s);
SQLCHAR const* as_sqlchar_str(std::string const& s);
SQLCHAR const* as_sqlchar_str(std::string const&&) = delete;
Last edited on
Why not use an actual C++ wrapper around SQL, instead of trying to mush your C++ code through a C interface.
https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-introduction.html

Dhayden:

(SQLCHAR *)parameterString.c_str()

Worked fine! Thank You! I did not like to cast a const variable to an not const...

salem c:

Thank your for the link, but I am working with MSSQL not MYSQL. And Because I have the work instruction to do it this way for learning purposes. Understanding lagacy c++ code, that does not have any wrappers need to understand the topic... so I try to understand and "revive" my c++ knowledge. I did not code c++ for more than 10 Years...

Thank you all for your help!
10 years misses 2 major revisions to the language; c++11 and c++17 features both of which are good to know. If you have time, you will want to review these. I was in the same situation a few years ago, slowly catching up. I am sure that whatever you are using has a modern version but if you need to learn the legacy, that isn't useful info.
Registered users can post here. Sign in or register to post.