Forum: wxRuby Problem with special characters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alejandro M. (Guest)
on 2009-02-20 17:21
(Received via mailing list)
Hi:



I am makeing a basic program with wxruby.

Is a frame with a Wx::Notebook inside.

The notebook has two tabs.



The first tab has a grid with 3 columns.



I am using dbi to load data from database, to fill the rows.

But the column  3 that is a description, is not fill when the
description
has special characters like ‘ç, é, ñ’;



But dbi returns the special characters, i create a file to write the
returned data for test.

And when i edit the file the text are ok.



My program is  :



require 'wx';

require "dbi";



class BasicGridApp < Wx::App

    def on_init

        begin

            @frame = Wx::Frame.new( nil , -1 , "Basic Grid Application",

                                     Wx::Point.new(10,10),

                                     Wx::Size.new(760,550) );

            @note  = Wx::Notebook.new( @frame, -1, Wx::Point.new(0,0),
Wx::Size.new(400,400), 0,

                                        "NoteBook" );

            @TabWindow = Wx::Window.new( @note, -1, Wx::Point.new(0,0),
Wx::Size.new(100,100), 0, "Tab1");

            @TabWindow1 = Wx::Window.new( @note, -1, Wx::Point.new(0,0),
Wx::Size.new(100,100), 0, "Tab2");



            @grid = Wx::Grid.new( @TabWindow , -1, Wx::Point.new(0,0),
Wx::Size.new(400,400), 0, "Grid");

            @grid.create_grid( 40 , 4 );



            @grid.set_col_label_value( 0, "ID" );

            @grid.set_col_label_value( 1, "Avreviação" );

            @grid.set_col_label_value( 2, "Descrição" );

            @grid.set_col_label_size( 24 );

            @grid.set_row_label_size( 24 );



            # connect to the MySQL server

            dbh = DBI.connect("DBI:Mysql:test:localhost", "test",
"er5aa5h");



            sth = dbh.execute("SELECT iduf, ufdesc, descricao COLLATE
latin1_bin AS descricao FROM uf ");



            contador = 0;

            sth.each do |row|

                @grid.set_col_format_number( 0 );



                @grid.set_cell_value( contador,  0,  row[0].to_s ); # ID

                @grid.set_cell_value( contador,  1,  row[1] );      #
short
description

                @grid.set_cell_value( contador,  2,  row[2] );      #
description

                contador += 1;

            end



            sth.finish;

        rescue DBI::DatabaseError => e

            puts "An error occurred";

            puts "Error code: #{e.err}";

            puts "Error message: #{e.errstr}";

        ensure

            # disconnect from server

            dbh.disconnect if dbh;

        end



        @note.add_page( @TabWindow,  "Titulo1", true, -1);

        @note.add_page( @TabWindow1,  "Titulo2", false, -1);

        @frame.show();

    end

end



b = BasicGridApp.new();

b.main_loop();



Can you give my some ligth?



Thanks in advance





  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090219-0, 19/02/2009
Tested on: 20/02/2009 11:14:33
avast! - copyright (c) 1988-2009 ALWIL Software.
Alex F. (Guest)
on 2009-02-20 21:20
(Received via mailing list)
>
> I am using dbi to load data from database, to fill the rows.
>
> But the column 3 that is a description, is not fill when the
> description has special characters like ‘ç, é, ñ’;
>

A wxRuby application can display any character of any script - but any
string that's passed into wxRuby must be UTF-8.

I would guess that your database is using some 8-bit encoding like
iso-8859-1. You should either:

1) convert your database to use utf-8 internally. This is the best
solution if you're able to design it from the start.

2) convert strings coming from the database to utf-8, using Ruby's
standard 'Iconv' library, or, if you're using Ruby 1.9, new string
methods like "encode!".

hth
alex
Alejandro M. (Guest)
on 2009-02-20 22:49
(Received via mailing list)
Alex :



Thanks is working.



I have a question



descricao = Iconv.iconv( 'utf-8', 'cp1252', row[2] );



@grid.set_cell_value( contador,  2, descricao.to_s );



What i need make to_s  to the result of Iconv.iconv?



If i put descricao wihtout to_s the set_cell_value give error
“set_cell_value:  can't convert Array into String (TypeError)”



Iconv.iconv returns array type?



Thanks in advance











De: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] Em nome de Alex F.
Enviada em: sexta-feira, 20 de fevereiro de 2009 16:06
Para: General discussion of wxRuby
Assunto: Re: [wxruby-users] Problem with special characters





>
> I am using dbi to load data from database, to fill the rows.
>
> But the column 3 that is a description, is not fill when the
> description has special characters like ‘ç, é, ñ’;
>

A wxRuby application can display any character of any script - but any
string that's passed into wxRuby must be UTF-8.

I would guess that your database is using some 8-bit encoding like
iso-8859-1. You should either:

1) convert your database to use utf-8 internally. This is the best
solution if you're able to design it from the start.

2) convert strings coming from the database to utf-8, using Ruby's
standard 'Iconv' library, or, if you're using Ruby 1.9, new string
methods like "encode!".

hth
alex
_______________________________________________
wxruby-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/wxruby-users




  _____

avast! Antivirus <http://www.avast.com> : Inbound message clean.

Virus Database (VPS): 090219-0, 19/02/2009
Tested on: 20/02/2009 16:45:44
avast! - copyright (c) 1988-2009 ALWIL Software.





  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090219-0, 19/02/2009
Tested on: 20/02/2009 17:27:11
avast! - copyright (c) 1988-2009 ALWIL Software.
Mario S. (Guest)
on 2009-02-21 04:33
(Received via mailing list)
On Fri, Feb 20, 2009 at 2:27 PM, (gmail) Alejandro Michelin Salomon <
removed_email_address@domain.invalid> wrote:

>
>
> descricao = Iconv.iconv( 'utf-8', 'cp1252', row[2] );
>
>
>
> @grid.set_cell_value( contador,  2, descricao.to_s );
>

The thing about Iconv.iconv() is that it always returns an array of
strings,
even if you just pass 1 string, so if your just passing 1 string, then
you
need to use @grid.set_cell_value( contador, 2, descricao[0]).


hth,

Mario
Alejandro M. (Guest)
on 2009-02-21 14:19
(Received via mailing list)
Mario :



Works fine.

Thanks a lot.





De: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] Em nome de Mario S.
Enviada em: sexta-feira, 20 de fevereiro de 2009 23:29
Para: General discussion of wxRuby
Assunto: Re: [wxruby-users] RES: Problem with special characters





On Fri, Feb 20, 2009 at 2:27 PM, (gmail) Alejandro Michelin Salomon
<removed_email_address@domain.invalid> wrote:

Alex :



Thanks is working.



I have a question



descricao = Iconv.iconv( 'utf-8', 'cp1252', row[2] );



@grid.set_cell_value( contador,  2, descricao.to_s );


The thing about Iconv.iconv() is that it always returns an array of
strings,
even if you just pass 1 string, so if your just passing 1 string, then
you
need to use @grid.set_cell_value( contador, 2, descricao[0]).


hth,

Mario


--
Mario S.
http://www.trilake.net
http://www.ruby-im.net
http://rubyforge.org/projects/wxruby/
http://rubyforge.org/projects/wxride/




  _____

avast! Antivirus <http://www.avast.com> : Inbound message clean.

Virus Database (VPS): 090220-0, 20/02/2009
Tested on: 21/02/2009 09:02:31
avast! - copyright (c) 1988-2009 ALWIL Software.





  _____

avast! Antivirus <http://www.avast.com> : Outbound message clean.


Virus Database (VPS): 090220-0, 20/02/2009
Tested on: 21/02/2009 09:15:26
avast! - copyright (c) 1988-2009 ALWIL Software.
This topic is locked and can not be replied to.