mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
readme: update web tutorial, fix code (#18989)
This commit is contained in:
parent
b25288338c
commit
2266ccecf3
@ -49,7 +49,7 @@ V projects can be created anywhere and don't need to have a certain structure:
|
|||||||
```bash
|
```bash
|
||||||
mkdir blog
|
mkdir blog
|
||||||
cd blog
|
cd blog
|
||||||
v init
|
touch blog.v
|
||||||
```
|
```
|
||||||
|
|
||||||
First, let's create a simple hello world website:
|
First, let's create a simple hello world website:
|
||||||
@ -203,11 +203,11 @@ since a DB connection doesn't have to be set up for each request.
|
|||||||
// blog.v
|
// blog.v
|
||||||
fn main() {
|
fn main() {
|
||||||
mut app := App{
|
mut app := App{
|
||||||
db: sqlite.connect(':memory:') or { panic(err) }
|
db: sqlite.connect(':memory:')!
|
||||||
}
|
}
|
||||||
sql app.db {
|
sql app.db {
|
||||||
create table Article
|
create table Article
|
||||||
}
|
}!
|
||||||
|
|
||||||
first_article := Article{
|
first_article := Article{
|
||||||
title: 'Hello, world!'
|
title: 'Hello, world!'
|
||||||
@ -222,7 +222,7 @@ fn main() {
|
|||||||
sql app.db {
|
sql app.db {
|
||||||
insert first_article into Article
|
insert first_article into Article
|
||||||
insert second_article into Article
|
insert second_article into Article
|
||||||
}
|
}!
|
||||||
vweb.run(app, 8080)
|
vweb.run(app, 8080)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -242,7 +242,7 @@ struct Article {
|
|||||||
pub fn (app &App) find_all_articles() []Article {
|
pub fn (app &App) find_all_articles() []Article {
|
||||||
return sql app.db {
|
return sql app.db {
|
||||||
select from Article
|
select from Article
|
||||||
}
|
} or { panic(err) }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -289,7 +289,7 @@ For example, if we only wanted to find articles with ids between 100 and 200, we
|
|||||||
|
|
||||||
return sql app.db {
|
return sql app.db {
|
||||||
select from Article where id >= 100 && id <= 200
|
select from Article where id >= 100 && id <= 200
|
||||||
}
|
} or { panic(err) }
|
||||||
```
|
```
|
||||||
|
|
||||||
Retrieving a single article is very simple:
|
Retrieving a single article is very simple:
|
||||||
@ -299,7 +299,7 @@ Retrieving a single article is very simple:
|
|||||||
pub fn (app &App) retrieve_article() ?Article {
|
pub fn (app &App) retrieve_article() ?Article {
|
||||||
return sql app.db {
|
return sql app.db {
|
||||||
select from Article limit 1
|
select from Article limit 1
|
||||||
}
|
} or { panic(err) }[0]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -308,9 +308,8 @@ bad queries will always be handled by the developer:
|
|||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
// article.v
|
// article.v
|
||||||
article := app.retrieve_article(10) or {
|
article := app.retrieve_article() or {
|
||||||
app.text('Article not found')
|
return app.text('Article not found')
|
||||||
return
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -349,7 +348,7 @@ pub fn (mut app App) new_article(title string, text string) vweb.Result {
|
|||||||
println(article)
|
println(article)
|
||||||
sql app.db {
|
sql app.db {
|
||||||
insert article into Article
|
insert article into Article
|
||||||
}
|
} or { panic(err) }
|
||||||
return app.redirect('/')
|
return app.redirect('/')
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -402,7 +401,7 @@ If one wants to persist data they need to use a file instead of memory SQLite Da
|
|||||||
Replace the db setup code with this instead:
|
Replace the db setup code with this instead:
|
||||||
|
|
||||||
```
|
```
|
||||||
db: sqlite.connect('blog.db') or { panic(err) }
|
db: sqlite.connect('blog.db')!
|
||||||
```
|
```
|
||||||
|
|
||||||
As we can see it attempts to open a file in the current directory named `blog.db`.
|
As we can see it attempts to open a file in the current directory named `blog.db`.
|
||||||
|
Loading…
Reference in New Issue
Block a user