Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="Main">
2
 
3
<h1>Creating Database</h1>
4
 
5
<p>
6
Most Web applications use database to keep data. Our blog system is not an exception. In this section, we will describe how to write database-driven pages for our blog system. We will use techniques including <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.DAO">database access object (DAO)</a> and <a href="http://www.pradosoft.com/demos/quickstart/?page=Database.ActiveRecord">Active Record</a>.
7
</p>
8
 
9
<p>
10
For tutorial purpose, we have simplified the requirements of our blog system so that it only needs to deal with user and post data. We thus create two database tables, <tt>users</tt> and <tt>posts</tt>, as shown in the following entity-relationship (ER) diagram.
11
</p>
12
 
13
<img src="<%~ ER.gif %>" class="output" />
14
 
15
<p>
16
We use a SQLite 3 database to keep our data. We first convert the ER diagram into the following SQL statements and save them in the file <tt>protected/schema.sql</tt>.
17
</p>
18
 
19
<com:TTextHighlighter CssClass="source" Language="sql">
20
/* create users table */
21
CREATE TABLE users (
22
  username      VARCHAR(128) NOT NULL PRIMARY KEY,
23
  email         VARCHAR(128) NOT NULL,
24
  password      VARCHAR(128) NOT NULL,  /* in plain text */
25
  role          INTEGER NOT NULL,       /* 0: normal user, 1: administrator */
26
  first_name    VARCHAR(128),
27
  last_name     VARCHAR(128)
28
);
29
 
30
/* create posts table */
31
CREATE TABLE posts (
32
  post_id       INTEGER NOT NULL PRIMARY KEY,
33
  author_id     VARCHAR(128) NOT NULL
34
                CONSTRAINT fk_author REFERENCES users(username),
35
  create_time   INTEGER NOT NULL,       /* UNIX timestamp */
36
  title         VARCHAR(256) NOT NULL,  /* title of the post */
37
  content       TEXT,                   /* post body */
38
  status        INTEGER NOT NULL        /* 0: published; 1: draft; 2: pending; 2: denied */
39
);
40
 
41
/* insert some initial data records for testing */
42
INSERT INTO users VALUES ('admin', 'admin@example.com', 'demo', 1, 'Qiang', 'Xue');
43
INSERT INTO users VALUES ('demo', 'demo@example.com', 'demo', 0, 'Wei', 'Zhuo');
44
INSERT INTO posts VALUES (NULL, 'admin', 1175708482, 'first post', 'this is my first post', 0);
45
</com:TTextHighlighter>
46
 
47
<com:NoteBox>
48
The <tt>fk_author</tt> constraint is ignored by SQLite because SQLite does not support <a href="http://www.sqlite.org/omitted.html">foreign key constraint</a>. Nevertheless, we still keep the constraint there for the capability of porting our blog system to different DBMS. Also, in the above we are exploiting the fact that the <tt>posts.post_id</tt> field is <a href="http://www.sqlite.org/autoinc.html">auto-incremental</a> if we assign NULL to it.
49
</com:NoteBox>
50
 
51
<p>
52
We then use the <a href="http://www.sqlite.org/download.html">SQLite command line tool</a> to create the SQLite database. We create a directory <tt>protected/data</tt> to hold the SQLite database file. We now execute the following command under the directory <tt>protected/data</tt>:
53
</p>
54
 
55
<com:TTextHighlighter CssClass="source cli">
56
sqlite3 blog.db < ../schema.sql
57
</com:TTextHighlighter>
58
 
59
<p>
60
The database has been created as <tt>protected/data/blog.db</tt> and we shall see the following directories and files:
61
</p>
62
 
63
<img src="<%~ directories.gif %>" class="output" />
64
 
65
<com:NoteBox>
66
It is required by SQLite that both the directory <tt>protected/data</tt> and the database file <tt>protected/data/blog.db</tt> be set writable by the Web server process.
67
</com:NoteBox>
68
 
69
</com:TContent>