Reputation: 63
While using SQLite in C++98, how to convert stringstream
to Unicode and pass it to sqlite3_exec()
? I constantly have this error:
error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>'
1> with"
Code:
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
tstringstream tstrsSQL;
tstrsSQL.imbue(std::locale("C"));
std::string s = tstrsSQL.str(); // converting stringstream to char?
const char* p = s.c_str();
/* Open database */
rc = sqlite3_open("db.db3", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}
/* Create SQL statement */
tstrsSQL << _T("SELECT") something ("FROM") << table;
/* Execute SQL statement */
rc = sqlite3_exec(db, p, callback, (void*)data, &zErrMsg); // p needs to be unicode
Upvotes: 3
Views: 3693
Reputation: 8313
You are using tstringstream
, which I guess uses std::wstringstream
if UNICODE
is defined, so its str()
gives a std::basic_string<wchar_t>
.
But you get the result into a std::string
, which is std::basic_string<char>
. So the assignment fails.
Anyway, you take the result and use it in sqlite3_exec()
, which takes a const char*
as input.
That's why you shouldn't use tstringstream
, you should use std::stringstream
, and remove the _T
from all of the string literals.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream strstream;
strstream << "Hello World";
string str = strstream.str();
cout << str << endl;
return 0;
}
Upvotes: 2