Search notes:

Perl module DBD::SQLite - testing utf8

Apparently, in order to properly use utf8 with DBD::SQLite, the database should be connected with my $dbh = DBI->connect('dbi::SQLite:dbname=…', '', '', { sqlite_unicode => 1 } ):
#!/usr/bin/perl
use warnings;
use strict;
use utf8;

use DBI;

unlink 'utf8.db' if -e 'utf8.db';

my $dbh = connect_db();

$dbh -> do ('create table utf8_test(txt)');

$dbh -> do ('insert into utf8_test values ("ä")');

my $sth = $dbh->prepare('insert into utf8_test values (?)');
$sth -> execute('ö');

$dbh->disconnect;

system ("sqlite3 utf8.db 'insert into utf8_test values(\"ü\")'");
system ('sqlite3 utf8.db "select * from utf8_test order by txt" > utf8.out');

open (my $out, '<:utf8', 'utf8.out');
my $line;
$line = <$out>; chomp $line; die unless $line eq 'ä';
$line = <$out>; chomp $line; die unless $line eq 'ö';
$line = <$out>; chomp $line; die unless $line eq 'ü';
close $out;

open ($out, '>:utf8', 'sel.out');
$dbh = connect_db();
$sth = $dbh->prepare('select * from utf8_test order by txt');
$sth->execute;
while (my ($val) = $sth->fetchrow_array) {
  print $out "$val\n";
}
close $out;

sub connect_db {
  my $dbh = DBI->connect( 'dbi:SQLite:dbname=utf8.db', '', '', { sqlite_unicode => 1 } ) or die "Could not create utf8.db";
  return $dbh;
}
Github repository PerlModules, path: /DBD/SQLite/utf8.pl

See also

DBD::SQLite,
SQLite
Perl and utf8

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758200707, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/modules/DBD/SQLite/utf8(86): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78