Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a script for reading in questions automatically from a txt and …
…added some more comments
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/perl -w | ||
|
||
################################################################################# | ||
# DatabaseCreate.pl # | ||
# # | ||
# will read in questions from questions.txt and dump them into the database # | ||
################################################################################# | ||
|
||
|
||
use DBI; | ||
use strict; | ||
|
||
my $dbh = DBI->connect("dbi:SQLite:dbname=Trivia.db","","") or die "Can't open DB: $!"; | ||
|
||
open(DAT, "questions.txt") || die("Could not open file!"); | ||
my @raw_data=<DAT>; | ||
close(DAT); | ||
|
||
print "Starting transfer...\n"; | ||
|
||
foreach my $question (@raw_data) | ||
{ | ||
chomp($question); | ||
(my $q, my $a) = split(/\*/, $question); | ||
$q = $dbh->quote($q); | ||
$a = $dbh->quote($a); | ||
|
||
my $sql = qq{INSERT INTO questions (question,answer) values($q, $a)}; | ||
print "About to execute [$sql]\n"; | ||
|
||
$dbh->do($sql) or die $dbh->errstr; | ||
} | ||
|
||
print "Transfer complete.\n"; |