1: #!/usr/local/bin/perl -w
2:
3: =head1 NAME
4:
5: mkdb.pl - a script for creation of new database.
6:
7: =head1 SYNOPSIS
8:
9: mkdb.pl
10:
11:
12: =head1 DESCRIPTION
13:
14: This script will create tables Questions and Tournaments
15: in the B<chgk> databse. If the tables exist, it will ask user whether
16: new tables should be created.
17:
18: =head1 BUGS
19:
20: The database, user and password are hardcoded.
21:
22: =head1 AUTHOR
23:
24: Dmitry Rubinstein
25:
26: =head1 $Id: mkdb.pl,v 1.8 2000/11/11 00:23:20 boris Exp $
27:
28: =cut
29:
30:
31: use DBI;
32: use strict;
33: my (@tbl_list, $dbh);
34:
35: sub CheckTable
36: {
37: my ($TabName) = @_;
38: my ($ans);
39:
40: if (scalar(grep(/^$TabName$/, @tbl_list))) {
41: print "Table $TabName exists. Do you want to delete it? ";
42: $ans = <STDIN>;
43: if ($ans =~ /[yY]/) {
44: $dbh->do("DROP TABLE $TabName");
45: print "deleted table $TabName\n";
46: } else {
47: exit;
48: }
49: }
50: }
51:
52: MAIN:
53: {
54: print "Before connecting to the DB\n";
55:
56: $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
57: or die "Can't connect to DB chgk\n" . $dbh->errstr;
58: print "Connected successfully\n";
59:
60: @tbl_list = $dbh->func( '_ListTables' );
61:
62: &CheckTable("Questions");
63: $dbh->do("CREATE TABLE Questions (
64: QuestionId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
65: KEY QuestionIdKey (QuestionId),
66: ParentId SMALLINT UNSIGNED NOT NULL,
67: KEY ParentIdKey (ParentId),
68: Number SMALLINT UNSIGNED NOT NULL,
69: KEY NumberKey (Number),
70: Type TEXT NOT NULL,
71: Question TEXT,
72: Answer TEXT,
73: Authors TEXT,
74: Sources TEXT,
75: Comments TEXT,
76: Rating TEXT
77: )")
78: or die "Can't create Questions table: $!\n";
79:
80: &CheckTable("Tournaments");
81:
82: $dbh->do("CREATE TABLE Tournaments (
83: Id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
84: KEY IdKey (Id),
85: ParentId INT UNSIGNED NOT NULL,
86: KEY ParentIdKey (ParentId),
87: Title TINYTEXT NOT NULL,
88: QuestionsNum INT UNSIGNED DEFAULT 0,
89: Type ENUM('ç','ô','þ'),
90: Copyright TEXT,
91: Info TEXT,
92: URL TINYTEXT,
93: FileName CHAR(25),
94: Editors TEXT,
95: EnteredBy TEXT,
96: PlayedAt DATE,
97: CreatedAt DATE NOT NULL
98: )")
99: or die "Can't create Tournaments table: $!\n";
100:
101: # CREATE INDEX ParentInd ON Tournaments (ParentId)
102: # CREATE UNIQUE INDEX IdInd ON Tournaments (Id)
103:
104: $dbh->disconnect;
105: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>