Skip to content

Instantly share code, notes, and snippets.

@epiphyllum
Created August 16, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epiphyllum/e9e080d08876066bda9b670491f47fca to your computer and use it in GitHub Desktop.
Save epiphyllum/e9e080d08876066bda9b670491f47fca to your computer and use it in GitHub Desktop.
gbk dbi and mojolicious
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use lib "/home/app/perl5/lib/perl5/";
use Encode;
use Data::Dumper;
use Getopt::Long;
use Mojolicious::Lite;
use XML::Hash;
use Data::Dumper;
use DBI;
####################################################################################################
# create table test(name TEXT)
####################################################################################################
my $dbh = DBI->connect('dbi:SQLite:citicdb');
$dbh->{RaiseError} = 1;
my $insert_sth = $dbh->prepare("insert into test(name) values(?)");
my $select_sth = $dbh->prepare("select * from test");
$insert_sth->execute("中国");
$select_sth->execute();
my $row = $select_sth->fetchrow_hashref();
print $row->{name} . "\n";
get "/gbk-dbi" => sub {
shift->render(data => encode("gbk", decode("utf8", $row->{name})));
};
get "/gbk-lit" => sub {
shift->render(data => encode("gbk", "中国"));
};
app->start;
@epiphyllum
Copy link
Author

从数据库里选出的字段需要decode("utf8")下才能变成字面量

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment