init_database v0.3.8

init_mysql_database改名为init_database。
以后可以扩展到其他数据库。
另外更改了输出的错误信息。

#!/usr/bin/perl -w
# file:init_database
# Init database.
# by:1e0n 05/10/28-v0.3.8
use strict;
use DBI();
use read_conf qw($user $pwd);

# Connect to the database.
print "init_mysql: Connect to the database\n";
my $dbh = DBI->connect("DBI:mysql:database=prime_db;host=localhost",
                       $user,
                          $pwd,
                          {’PrintError’ => 0})
        or die <<EOF;
Connect to database fail:
    Maybe you havn’t configure primedb.conf correctly?
    See README && prime.conf for more information.
EOF

# Drop table ‘prime_table’.
$dbh->do("DROP TABLE prime_table");

# Create a new table ‘prime_table’.
# This’s the table we store the prime series.
print "init_mysql: Init table\n";
$dbh->do("CREATE TABLE prime_table (id                    INTEGER,
                                    prime_n                INTEGER,
                                    prime_step_id        INTEGER,
                                    prime_step_n        INTEGER)");

# Insert init data into ‘prime_table’,that’s:
# +—–+——-+————-+————+
# | id  |prime_n|prime_step_id|prime_step_n|
# +—–+——-+————-+————+
# |  1  |   2   |      1      |      1     | first prime is 2,and sqrt(2) is 1
# |  2  |   3   |      1      |      1     | second prime is 3,and sqrt(3) is 1
# +—–+——-+————-+————+
print "init_mysql: Init data\n";
$dbh->do("INSERT INTO prime_table VALUES(1,2,1,1)");
$dbh->do("INSERT INTO prime_table VALUES(2,3,1,1)");

# Now all finish.
# Disconnect from the database.
print "init_mysql: Now database is ready\n";
$dbh->disconnect();

Leave a Reply