Advertisement
kijato

sqlite_in_memory

Feb 12th, 2020
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.48 KB | None | 0 0
  1. # examples/sqlite_in_memory.pl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper qw(Dumper);
  6. use DBI;
  7.  
  8. my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:");
  9. $dbh->do("CREATE TABLE words (
  10.  id     INTEGER PRIMARY KEY,
  11.  word   VARCHAR(255)
  12. )");
  13.  
  14. for my $word ("abc", "def", "ghi") {
  15.     $dbh->do("INSERT INTO words (word) VALUES (?)", undef, $word);
  16. }
  17.  
  18. my $sth = $dbh->prepare("SELECT * from words");
  19. $sth->execute;
  20. while (my $h = $sth->fetchrow_hashref) {
  21.     print Dumper $h;
  22. }
  23.  
  24.  
  25. __END__
  26.  
  27.  
  28. Output:
  29.  
  30. $VAR1 = {
  31.           'id' => 1,
  32.           'word' => 'abc'
  33.         };
  34. $VAR1 = {
  35.           'id' => 2,
  36.           'word' => 'def'
  37.         };
  38. $VAR1 = {
  39.           'word' => 'ghi',
  40.           'id' => 3
  41.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement