tutorials: blog: use db.create_table()

pull/9622/head
Alexander Medvednikov 2021-04-06 22:15:14 +03:00
parent f4566bb324
commit 89838f2e21
3 changed files with 12 additions and 9 deletions

View File

@ -33,8 +33,11 @@ pub fn (app &App) index() vweb.Result {
pub fn (mut app App) init_once() { pub fn (mut app App) init_once() {
app.db = sqlite.connect('blog.db') or { panic(err) } app.db = sqlite.connect('blog.db') or { panic(err) }
app.db.exec('create table if not exists article (' + 'id integer primary key, ' + app.db.create_table('article', [
"title text default ''," + "text text default ''" + ');') 'id integer primary key',
"title text default ''",
"text text default ''",
])
} }
pub fn (mut app App) init() { pub fn (mut app App) init() {

View File

@ -36,21 +36,21 @@ pub mut:
} }
// //
fn C.sqlite3_open(charptr, &&C.sqlite3) int fn C.sqlite3_open(&char, &&C.sqlite3) int
fn C.sqlite3_close(&C.sqlite3) int fn C.sqlite3_close(&C.sqlite3) int
// //
fn C.sqlite3_prepare_v2(&C.sqlite3, charptr, int, &&C.sqlite3_stmt, &charptr) int fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int
fn C.sqlite3_step(&C.sqlite3_stmt) int fn C.sqlite3_step(&C.sqlite3_stmt) int
fn C.sqlite3_finalize(&C.sqlite3_stmt) int fn C.sqlite3_finalize(&C.sqlite3_stmt) int
// //
fn C.sqlite3_column_name(&C.sqlite3_stmt, int) charptr fn C.sqlite3_column_name(&C.sqlite3_stmt, int) &char
fn C.sqlite3_column_text(&C.sqlite3_stmt, int) byteptr fn C.sqlite3_column_text(&C.sqlite3_stmt, int) &byte
fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int
@ -61,7 +61,7 @@ fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64
fn C.sqlite3_column_count(&C.sqlite3_stmt) int fn C.sqlite3_column_count(&C.sqlite3_stmt) int
// //
fn C.sqlite3_errstr(int) charptr fn C.sqlite3_errstr(int) &char
fn C.sqlite3_free(voidptr) fn C.sqlite3_free(voidptr)
@ -180,5 +180,5 @@ pub fn (db DB) insert<T>(x T) {
} }
pub fn (db DB) create_table(table_name string, columns []string) { pub fn (db DB) create_table(table_name string, columns []string) {
db.exec('create table $table_name (' + columns.join(',\n') + ')') db.exec('create table if not exists $table_name (' + columns.join(',\n') + ')')
} }

View File

@ -380,7 +380,7 @@ fn main() {
str_tmp_expr_advanced_var_decl() str_tmp_expr_advanced_var_decl()
str_inter() str_inter()
match_expr() match_expr()
// optional_str() optional_str()
// optional_return() // optional_return()
str_replace() str_replace()
str_replace2() str_replace2()