improve Continue db code: use table-exists? instead of catch all exn?
This commit is contained in:
parent
a2f75e494d
commit
19774260f7
|
@ -975,7 +975,8 @@ following to the top of our model:
|
||||||
|
|
||||||
We will use the following bindings from the @racketmodname[db]
|
We will use the following bindings from the @racketmodname[db]
|
||||||
library: @racket[connection?], @racket[sqlite3-connect],
|
library: @racket[connection?], @racket[sqlite3-connect],
|
||||||
@racket[query-exec], @racket[query-list], and @racket[query-value].
|
@racket[table-exists?], @racket[query-exec], @racket[query-list], and
|
||||||
|
@racket[query-value].
|
||||||
|
|
||||||
The first thing we should do is decide on the relational structure of
|
The first thing we should do is decide on the relational structure of
|
||||||
our model. We will use the following tables:
|
our model. We will use the following tables:
|
||||||
|
@ -1011,7 +1012,7 @@ We can now write the code to initialize a @racket[blog] structure:
|
||||||
(define (initialize-blog! home)
|
(define (initialize-blog! home)
|
||||||
(define db (sqlite3-connect #:database home #:mode 'create))
|
(define db (sqlite3-connect #:database home #:mode 'create))
|
||||||
(define the-blog (blog db))
|
(define the-blog (blog db))
|
||||||
(with-handlers ([exn? void])
|
(unless (table-exists? db "posts")
|
||||||
(query-exec db
|
(query-exec db
|
||||||
(string-append
|
(string-append
|
||||||
"CREATE TABLE posts "
|
"CREATE TABLE posts "
|
||||||
|
@ -1019,7 +1020,8 @@ We can now write the code to initialize a @racket[blog] structure:
|
||||||
(blog-insert-post!
|
(blog-insert-post!
|
||||||
the-blog "First Post" "This is my first post")
|
the-blog "First Post" "This is my first post")
|
||||||
(blog-insert-post!
|
(blog-insert-post!
|
||||||
the-blog "Second Post" "This is another post")
|
the-blog "Second Post" "This is another post"))
|
||||||
|
(unless (table-exists? db "comments")
|
||||||
(query-exec db
|
(query-exec db
|
||||||
"CREATE TABLE comments (pid INTEGER, content TEXT)")
|
"CREATE TABLE comments (pid INTEGER, content TEXT)")
|
||||||
(post-insert-comment!
|
(post-insert-comment!
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
(define (initialize-blog! home)
|
(define (initialize-blog! home)
|
||||||
(define db (sqlite3-connect #:database home #:mode 'create))
|
(define db (sqlite3-connect #:database home #:mode 'create))
|
||||||
(define the-blog (blog db))
|
(define the-blog (blog db))
|
||||||
(with-handlers ([exn? void])
|
(unless (table-exists? db "posts")
|
||||||
(query-exec db
|
(query-exec db
|
||||||
(string-append
|
(string-append
|
||||||
"CREATE TABLE posts "
|
"CREATE TABLE posts "
|
||||||
|
@ -22,7 +22,8 @@
|
||||||
(blog-insert-post!
|
(blog-insert-post!
|
||||||
the-blog "First Post" "This is my first post")
|
the-blog "First Post" "This is my first post")
|
||||||
(blog-insert-post!
|
(blog-insert-post!
|
||||||
the-blog "Second Post" "This is another post")
|
the-blog "Second Post" "This is another post"))
|
||||||
|
(unless (table-exists? db "comments")
|
||||||
(query-exec db
|
(query-exec db
|
||||||
"CREATE TABLE comments (pid INTEGER, content TEXT)")
|
"CREATE TABLE comments (pid INTEGER, content TEXT)")
|
||||||
(post-insert-comment!
|
(post-insert-comment!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user