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 |
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 #5355 is a reply to message #5354] |
Fri, 15 September 2006 20:23 |
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 #5373 is a reply to message #5358] |
Sat, 16 September 2006 16:32 |
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
|
|
|
Goto Forum:
Current Time: Fri May 03 12:52:56 CEST 2024
Total time taken to generate the page: 0.02398 seconds
|