2
#include <iostream> 
#include <Windows.h>
#include <mysql.h> 
#include <string> 
#include <stdio.h>


using namespace std; 

MYSQL *connection, mysql; 
MYSQL_RES *result; 
MYSQL_ROW row; 
int query_state; 


int main(int argc, char** argv)  
{ 
    setlocale(LC_ALL, "");
      mysql_init(&mysql);    
      mysql_real_connect(&mysql,"xxx","xxx","xxx","xxx",0,0,0); 

  mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8"); 
  mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES 'utf8' [COLLATE 'utf8_slovak_ci']"); 

  mysql_query(&mysql, "SELECT priezvisko FROM tour_pouzivatelia WHERE id_pouz=5"); 

  result = mysql_store_result(&mysql); 
  if ((row = mysql_fetch_row(result)) != NULL) {     
        string sql; 
        sql = "UPDATE tour_pouzivatelia SET priezvisko='"; 
        sql += row[0];
        sql += "' WHERE id_pouz=2";



        char* s = new char[sql.length()];


        strcpy(s, sql.c_str());

        mysql_query(&mysql, s); 
    } 


 getchar();
  return 0; 
}

The value in the database is +ľščťžýáíéô§ň, the tables have charset utf8 and ut8_slovak_ci collation

Yet the following code saves the result as +¾šèžýáíéúäô§ò.

I'm using Visual studio 2010 c++ express.

The problem seems to be that mysql_query does not handle the sql passed to it, it removes the specific characters like š,č,ľ. I've tried many conversions, the function itself however requires the sql to be as const char *.

Is there any way to make this work, so that the char * variable will save the given characters correctly?

If I read the values from database they show up correctly...

bardiir
  • 14,556
  • 9
  • 41
  • 66

1 Answers1

3

Before any DML, try to issue SET names utf8 query (or other which you need)

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • Wow, that helped for the content from the database so that works now (though I'd swear I tried that before), nvm thanks:) – user1143348 Jan 11 '12 at 14:48
  • although it still does not work for sql = "UPDATE tour_pouzivatelia SET priezvisko='aľšč' WHERE id_pouz=1;"; it just saves the a alone, not the rest of the characters – user1143348 Jan 11 '12 at 14:49
  • Did you have hard coded the UPDATE query?Maybe file is not in UTF8 encoding.Try check if VS2010 encodes file in UTF8 (even if show ok) http://stackoverflow.com/questions/696627/how-to-set-standard-encoding-in-visual-studio – rkosegi Jan 11 '12 at 18:20