SHARE
TWEET

encoding problems on BLOB fields

a guest May 19th, 2017 202 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. use v5.24;
  4. use warnings;
  5. use utf8;
  6.  
  7. use Mojo::mysql;
  8. use Data::Dumper;
  9. use Encode;
  10.  
  11.  
  12. my $db_name = $ARGV[0] // 'mydb';
  13.  
  14. my $mysql = Mojo::mysql->strict_mode("mysql://dbuser:dbuser\@localhost/$db_name");
  15. my $db = $mysql->db;
  16.  
  17. my $string = "Αλέξανδρος";
  18.  
  19. my $id = $db->insert('a_test',
  20.     {
  21.         name => $string,
  22.         data => encode_utf8($string),
  23.     }
  24. )->last_insert_id;
  25.  
  26. my $row = $db->select('a_test',
  27.     undef,
  28.     {
  29.         id => $id,
  30.     }
  31. )->hash     or die 'row not found';
  32.  
  33. print Dumper({
  34.     row => $row,
  35.     string => $string,
  36.     row_name_is_same_as_inserted => $row->{name} eq $string,
  37.     row_data_is_same_as_inserted => $row->{data} eq encode_utf8($string),
  38. });
  39.  
  40. __END__
  41.  
  42. MariaDB [mydb]> show create table a_test;
  43. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | Table  | Create Table                                                                                                                                                                                         |
  45. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | a_test | CREATE TABLE `a_test` (
  47.   `id` int(11) NOT NULL AUTO_INCREMENT,
  48.   `name` varchar(255) DEFAULT NULL,
  49.   `data` longblob,
  50.   PRIMARY KEY (`id`)
  51. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 |
  52. +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. 1 row in set (0.00 sec)
  54.  
  55. Output:
  56. $VAR1 = {
  57.           'row_data_is_same_as_inserted' => '',
  58.           'string' => "\x{391}\x{3bb}\x{3ad}\x{3be}\x{3b1}\x{3bd}\x{3b4}\x{3c1}\x{3bf}\x{3c2}",
  59.           'row_name_is_same_as_inserted' => 1,
  60.           'row' => {
  61.                      'id' => 5,
  62.                      'name' => "\x{391}\x{3bb}\x{3ad}\x{3be}\x{3b1}\x{3bd}\x{3b4}\x{3c1}\x{3bf}\x{3c2}",
  63.                      'data' => 'ÎλÎ<U+00AD>ξανδÏοÏ'
  64.                    }
  65.         };
RAW Paste Data
Top