#!/usr/bin/perl use DBI; use warnings; use strict; use feature 'say'; my @genre = qw/misc comics art recreation cooking literature science computer fiction fantasy travel encyclopedia/; push @genre, "science fiction"; my $DEBUG = 1; my $dbh = DBI->connect("dbi:SQLite:dbname=/home/miekg/etc/bib/biblio.db", "", "") or die "Can't connect"; sub book_info($) { split /\n/, `/home/miekg/bin/book_get $_[0]`; } sub sql_read($) { my $isbn = shift; my @data; my $sth = $dbh->prepare('SELECT * FROM books WHERE isbn = ?') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute($isbn) or die "Failed to execute statement: " . $sth->errstr; @data = $sth->fetchrow_array(); $data[7] = "" if $#data != -1;; #loc_code is not set return @data if $#data != -1; (); } sub sql_write($$$$$) { my ($isbn, $title, $author, $genre, $year) = @_; say "Title: $title"; say "Auteur: $author"; say "Genre: $genre"; say "Jaar: $year"; my $epoch = time(); my $sth = $dbh->prepare('INSERT INTO books VALUES (?, ?, ?, ?, ?, ?, ?, ?)') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute(undef, $isbn, "$title", "$author", "$genre", $year, $epoch, undef) or die "Couldn't insert book: " . $dbh->errstr; $sth->finish; } while(<>) { chomp; if (! /\d+/) { say "Need a number"; next; } my @db = sql_read $_; if ($#db != -1) { print "Boek $_ zit al in de database: "; say "@db[1,2]"; next; } my @book = book_info $_; if (!@book) { say "Kan book ook niet online vinden: $_"; next; } say "Boek ($_) gevonden, voeg het nu toe aan de database"; # Genre check if ($book[2] eq "none") { my $answer; say "Geef het juiste genre: "; for(my $i =0 ; $i < @genre; $i++) { printf "%2d %s\n", $i, $genre[$i]; } read STDIN, $answer, 2; $book[2] = $genre[$answer]; } sql_write $_, $book[0], $book[1], $book[2], $book[3]; } #print sql_read 9789090195612; #print sql_read 9789090195611;