Skip to content

Instantly share code, notes, and snippets.

@64lines
Last active January 1, 2016 04:29
Show Gist options
  • Save 64lines/8092229 to your computer and use it in GitHub Desktop.
Save 64lines/8092229 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use DBI;
my $database_name = "inmemoriam.dat";
my $dbh = DBI->connect(
"dbi:SQLite:dbname=$database_name",
"",
"",
{ RaiseError => 1 },
) or die $DBI::errstr;
sub drop_memory_table {
$dbh->do("DROP TABLE memory");
}
sub create_memory_table {
$dbh->do("CREATE TABLE memory(id INTEGER PRIMARY KEY, value TEXT, description TEXT, id_memory_parent INTEGER)");
}
sub insert_memory {
$dbh->do("INSERT INTO memory(id, value, description, id_memory_parent) VALUES (?, ?, ?, ?)",
undef, $_[0], $_[1], $_[2], $_[3]
);
}
sub find_memories {
my $sth = $dbh->prepare("SELECT * FROM memory;");
$sth->execute();
my $id = 0;
my $value = "";
my $description = "";
my $id_memory_parent = "";
while(($id, $value, $description, $id_memory_parent) = $sth->fetchrow()) {
print "Id: $id, Value: $value, Description: $description, Id Parent: $id_memory_parent\n";
}
$sth->finish();
}
sub auto_increment_memory_id {
my $sth = $dbh->prepare("SELECT max(id) + 1 FROM memory;");
$sth->execute();
my ($return_id) = $sth->fetchrow();
if (!$return_id){
$return_id = 1;
}
return $return_id;
}
&drop_memory_table;
&create_memory_table;
&insert_memory(1, "Ghost in the shell", "Is an anime...", undef);
my $memory = &auto_increment_memory_id;
print "--> Memory id: $memory";
$dbh->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment