Quantcast
Channel: MySQL Forums - Connector/C++
Viewing all articles
Browse latest Browse all 527

Problem with string encoding (no replies)

$
0
0
When I use connector/C++ to retrieve a string with non-ascii characters, I get wrong results. Using libmysqlclient, I get the expected string.
My client and SQL server both run on the same system (Ubuntu 11.10 with mysql 1.5.62-0 and libmysqlcppconn-dev 1.1.0-3).

The output of the connector program is:
Fétis, François-Joseph
The output of the mysqlclient program is:
Fétis, François-Joseph
(It might be that this forum garbles this output, but the second is correct).

Below are the programs used to generate the output. If someone could point out what I could do to get the right result, I would be grateful.

// cppconntst.cpp
#include "mysql_driver.h"
#include "cppconn/prepared_statement.h"
#include "cppconn/sqlstring.h"
#include <memory>
#include <stdio.h>

using std::unique_ptr;

int main()
{
char pw[31];
FILE *myp = fopen("myp","r");
if (fscanf(myp,"%30s",pw) !=1)
return 1;
fclose(myp);
sql::Driver *driver = get_driver_instance();
unique_ptr<sql::Connection> conn(driver->connect("localhost","geert",pw));
conn->setClientOption("characterSetResults","UTF8");
conn->setSchema("orpheus");
unique_ptr<sql::Statement> stmt(conn->createStatement());
stmt->execute("SET NAMES 'utf8'");
unique_ptr<sql::PreparedStatement> mdata(conn->prepareStatement(
"SELECT Creator FROM ORPtunes WHERE TuneID=? "));
sql::SQLString creator;
mdata->setInt(1, 4644);
unique_ptr<sql::ResultSet> res(mdata->executeQuery());
if (res->next()) {
creator=res->getString(1);
printf("%s\n", creator.c_str());
}
}


// mysqltst.c
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>

char * get_pw()
{
char pw[31];
FILE *myp = fopen("myp","r");
int success= (fscanf(myp,"%30s",pw) == 1);
fclose(myp);
return success ? strndup(pw,30) : 0;
}

int main(int argc, char **argv)
{
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
mysql_init(&mysql);
char *pw=get_pw();
if (mysql_real_connect(&mysql, "localhost", "geert", pw, "orpheus", 0, 0, 0)==0)
printf("Connection not succesful\n");
free(pw);
int status = mysql_query(&mysql,"SELECT creator FROM ORPtunes WHERE TuneID=4644");
if (status != 0) {
printf("Cannot perform query (%d)\n",status);
return 1;
}
result=mysql_store_result(&mysql);
if (result !=0) {
MYSQL_ROW row;
row = mysql_fetch_row(result);
printf("%s\n", row[0]);
mysql_free_result(result);
}
mysql_close(&mysql);
}

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>