http://www.perlmonks.org?node_id=11160474

jmClifford has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I have SQLite with the Perl, where some text values returned are Degrees C and Degrees F and the "Degrees" is a small super scripted °. And when this is returned to my Perl console it can show as °F. How can I easily convert / display this new font which I assume is not the usual ASCII (or my keyboard) font.

Regards JC.....

Replies are listed 'Best First'.
Re: SQLite returns funny characters
by choroba (Cardinal) on Jul 09, 2024 at 14:13 UTC
    You need to tell Perl and SQLite that you're using UTF-8:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use utf8; # Source code contains non-ASCII UTF-8 characters. use open IO => ':encoding(UTF-8)', ':std'; # Output should be UTF-8 e +ncoded. use DBI; use DBD::SQLite::Constants ':dbd_sqlite_string_mode'; # A constant us +ed below. my $dbh = 'DBI'->connect('dbi:SQLite:dbname=:memory:', "", "", { # This tells the db driver to use Unicode. sqlite_string_mode => DBD_SQLITE_STRING_MODE_UNICODE_STRICT }); $dbh->do('CREATE TABLE t1 (temperature INT, degree TEXT)'); my $insert = $dbh->prepare( 'INSERT INTO t1 (temperature, degree) VALUES (?, ?)'); $insert->execute(10, '°C'); $insert->execute(80, '°F'); my $select = $dbh->prepare('SELECT * FROM t1'); $select->execute; while (my @row = $select->fetchrow_array) { say "@row"; }
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks to Choroba for the contained sample. My issue now is:

      Can't continue after import errors "dbd_sqlite_string_mode" is not defined in %DBD::SQLite::Constants::EX +PORT_TAGS

      I can assess SQLite DBs with other Perl files. Is this a case of updating to the latest ? I don't think I have ever "CSPAN"ed a DBD module onto my Win10 system.

      Regards JC....

        What version of DBD::SQLite is installed?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: SQLite returns funny characters
by roho (Bishop) on Jul 10, 2024 at 04:51 UTC
    A quick note for Windows users. If you run the excellent sample code from choroba in a cmd window, you need to first change the code page to UTF for the output to display properly, as follows: chcp 65001

    "It's not how hard you work, it's how much you get done."