Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » my_sql's charset in linux
my_sql's charset in linux [message #5351] Fri, 15 September 2006 18:49 Go to next message
nicomesas is currently offline  nicomesas
Messages: 104
Registered: September 2006
Location: Barcelona, Spain
Experienced Member
I have been two days proving MySQL and have had a serious problem with charset.
I have a series of tables utf8 codified, and in principle it must not have very normal problems with the accents: áéíóú, ñ and other special characters in my language.
This is the sample:
#include <Core/Core.h>
#include <MySql/MySql.h>

CONSOLE_APP_MAIN
{
  String s = "SELECT nombre, refcontable FROM actividades_clases" ; 

  MySqlSession sesion ;
  if (sesion.Connect( "root", "mundoflipi", "skorg_dsrllo_v3", "localhost" ))
  {
    Sql q(sesion) ;
    q.Execute(theSelect) ;
    int row = 0;	
    while(q.Fetch())
    {
      String resp =   ;
      Cout() << "[" << row << "] " << resp << (String)q[0] << "  " << (String)q[1] << "\r\n" ;
      row++ ;
     }
     sesion.Close() ;
   }
}



Columns "nombre" can contain leters with accents, by example, my name is Nicolás.
There are a script to create the table:
CREATE TABLE `actividades_clases` (
  `code` varchar(22) collate utf8_spanish_ci NOT NULL default '',
  `nombre` varchar(30) collate utf8_spanish_ci default NULL,
  `dAdmin` varchar(2) collate utf8_spanish_ci default NULL,
  `isVentas` int(11) default NULL,
  `refContable` varchar(30) collate utf8_spanish_ci default NULL,
  PRIMARY KEY  (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
INSERT INTO `actividades_clases` VALUES  ('0070000000000002','NICOLÁS','D',0,'DHARMA'),
 ('0070000000000003','cúlturá','F',0,'CULTURAL'),
 ('0070000000000004','DOCENCIA','F',0,'DOCENCIA'),
 ('0070000000000005','VOLUNTARIADO','?F',0,'VOLUNTARIADO'),
 ('0070000000000006','RESIDENCIA','?F',0,'CONVIVENCIA'),
 ('0070000000000007','COMEDOR','?F',0,'CONVIVENCIA'),
 ('0706401e1a6607402b3500','VENTAS','F',1,'VENTAS FUNDACIÓN'),
 ('0706401e10216c0f1c6100','ARTICULOS DEL REFUGIO','F',1,'REFUGIO');


After thinking much, to prove it in Windows (and it works correctly), to spend to me all the day of yesterday reading documentation of MySQL.com, and mainly, to dive by the code of the UPP, I believe that I have found the solution.

By some reason my MySQL does not return the characters to me in UTF, although I hoped that I took root outside. Possibly he would have to change the configuration in the server.

But the case is that there is a function of the API of MySQL that serves to change the codification of a given connection. This function is mysql_set_character_set.

Simply it would have to call to that function after establishing the connection

  MySqlSession sesion ;
  if (sesion.Connect( "root", "mundoflipi", "skorg_dsrllo_v3", "localhost" ))
  {
    mysql_set_character_set( sesion, "utf8") ;

    Sql q(sesion) ;
    q.Execute(theSelect) ;

Perhaps the MySQLSession object would have to be in charge to compare the codification that comes by defect in the server with defined by the system or the present language, that we can obtain with this code:
String sysCharSetName = CharsetName(GetDefaultCharset());

Nico

[Updated on: Fri, 15 September 2006 18:51]

Report message to a moderator

Re: my_sql's charset in linux [message #5352 is a reply to message #5351] Fri, 15 September 2006 18:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Can you try that?

Well, In fact, I would feel even safer with MySql set to fixed encoding (most likely "utf-8") and perform conversion in U++ ("ToCharset").

Mirek
Re: my_sql's charset in linux [message #5354 is a reply to message #5352] Fri, 15 September 2006 19:17 Go to previous messageGo to next message
nicomesas is currently offline  nicomesas
Messages: 104
Registered: September 2006
Location: Barcelona, Spain
Experienced Member
OK, today and tomorrow I will continue testing the database and will try to use the function "ToCharset"
Nico
Re: my_sql's charset in linux [message #5355 is a reply to message #5354] Fri, 15 September 2006 20:23 Go to previous messageGo to next message
nicomesas is currently offline  nicomesas
Messages: 104
Registered: September 2006
Location: Barcelona, Spain
Experienced Member
Hello,

Already I have test it and seems that it works well.

The problem is that don't know how to change the codification of my system very well. What I have done has been to say to him to the TheIDE that that charset is another one (for example iso8859-15) and at the beginning of the program to establish the codification by hand.

Nico

#include <Core/Core.h>
#include <MySql/MySql.h>

CONSOLE_APP_MAIN
{
  // GetDefaultCharset() ; allways returns CHARSET_UTF8 in my pc
  // If I want to change the codification I must make it in TheIDE and in addition establish 
  // it by hand to see if the result is correct.
  //SetDefaultCharset(CHARSET_ISO8859_15) ;
  // if you uncomment this line, you must change in the ide the default char set, and then
  // the result is ok


  int charSet = GetDefaultCharset() ;
  Cout() << "charset:" <<CharsetName( charSet )  << "\n" ;
  String 	s = "SELECT nombre, refcontable FROM actividades_clases" ; 

  MySqlSession sesion ;
  if (sesion.Connect( "root", "mundoflipi", "skorg_dsrllo_v3", "localhost" ))
  {
    mysql_set_character_set( sesion, "utf8") ;

    Sql q(sesion) ;
    q.Execute(s) ;
    int row = 0;	
    while(q.Fetch())
    {
	String f0 = ToCharset(charSet, (String)q[0], CHARSET_UTF8, '?') ;
	String f1 = ToCharset(charSet, (String)q[1], CHARSET_UTF8, '?') ;
			
	Cout() << "[" << row << "  source]" << (String)q[0] << "  " << (String)q[1] << "\r\n" ;
	Cout() << "[" << row << " encoded]" << f0 << "  " << f1 << "\r\n" ;
	row++ ;
     }
     sesion.Close() ;
   }
}

[Updated on: Fri, 15 September 2006 20:24]

Report message to a moderator

Re: my_sql's charset in linux [message #5356 is a reply to message #5355] Fri, 15 September 2006 20:53 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
nicomesas wrote on Fri, 15 September 2006 14:23


The problem is that don't know how to change the codification of my system very well. What I have done has been to say to him to the TheIDE that that charset is another one (for example iso8859-15) and at the beginning of the program to establish the codification by hand.


I guess it is understandable - compiled code cannot know anything about TheIDE setting. So changeing in both places is actually the right thing.

Mirek
Re: my_sql's charset in linux [message #5357 is a reply to message #5356] Fri, 15 September 2006 21:15 Go to previous messageGo to next message
nicomesas is currently offline  nicomesas
Messages: 104
Registered: September 2006
Location: Barcelona, Spain
Experienced Member
I believe that my English is every time worse. Sad Embarassed

I am going away to try to explain.

To the console program I have established him in Run Options stdout to a file.

When doing this, I believe that TheIde creates a file with codification that it has defined, for that reason to see if it changes well I must establish the DefaultCharset by hand, (I think)

Nico
Re: my_sql's charset in linux [message #5358 is a reply to message #5357] Fri, 15 September 2006 21:31 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
nicomesas wrote on Fri, 15 September 2006 15:15

I believe that my English is every time worse. Sad Embarassed



Actually, I am afraid you are right. But I will try (to undestand).

Quote:


To the console program I have established him in Run Options stdout to a file.

When doing this, I believe that TheIde creates a file with codification that it has defined, for that reason to see if it changes well I must establish the DefaultCharset by hand, (I think)




No, IDE just does stdout redirection, it does not filter characters in any way. At least I believe so....

[Updated on: Fri, 15 September 2006 21:31]

Report message to a moderator

Re: my_sql's charset in linux [message #5373 is a reply to message #5358] Sat, 16 September 2006 16:32 Go to previous message
nicomesas is currently offline  nicomesas
Messages: 104
Registered: September 2006
Location: Barcelona, Spain
Experienced Member
I believe that already I understand it...

It is clear that who creates the file stdout is the console program, and she builds it in the codification that we say to him. I explained myself bad.

What wanted to express is that to see this result correctly we must put the IDE in the same codification.

Nico
Previous Topic: Multiple fields primary key
Next Topic: SqlCtrl error linux
Goto Forum:
  


Current Time: Fri May 03 12:52:56 CEST 2024

Total time taken to generate the page: 0.02398 seconds