1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

db.sqlite: make functions return results, breaking change (#19093)

This commit is contained in:
jacksonmowry
2023-08-10 02:39:32 +00:00
committed by GitHub
parent d0cc564089
commit 76b4c92848
8 changed files with 75 additions and 81 deletions

View File

@@ -2,20 +2,19 @@ import db.sqlite
fn main() {
db := sqlite.connect(':memory:')!
db.exec("create table users (id integer primary key, name text default '');")
db.exec("create table users (id integer primary key, name text default '');") or { panic(err) }
db.exec("insert into users (name) values ('Sam')")
db.exec("insert into users (name) values ('Peter')")
db.exec("insert into users (name) values ('Kate')")
db.exec("insert into users (name) values ('Sam')")!
db.exec("insert into users (name) values ('Peter')")!
db.exec("insert into users (name) values ('Kate')")!
nr_users := db.q_int('select count(*) from users')
nr_users := db.q_int('select count(*) from users')!
println('nr users = ${nr_users}')
name := db.q_string('select name from users where id = 1')
name := db.q_string('select name from users where id = 1')!
assert name == 'Sam'
users, code := db.exec('select * from users')
println('SQL Result code: ${code}')
users := db.exec('select * from users')!
for row in users {
println(row.vals)
}

View File

@@ -44,8 +44,8 @@ pub fn (mut app App) index() vweb.Result {
fn (mut app App) update_db() !int {
mut db := mydb()!
db.exec('INSERT INTO visits (created_at) VALUES ("")')
visits := db.q_int('SELECT count(*) FROM visits')
db.exec('INSERT INTO visits (created_at) VALUES ("")')!
visits := db.q_int('SELECT count(*) FROM visits')!
db.close()!
return visits
}
@@ -56,10 +56,10 @@ fn main() {
println('`v -d vweb_livereload watch --keep run examples/vwatch/web_server/`')
println('')
mut db := mydb()!
db.exec('CREATE TABLE visits (id integer primary key AUTOINCREMENT, created_at timestamp default current_timestamp);')
db.exec('CREATE TABLE visits (id integer primary key AUTOINCREMENT, created_at timestamp default current_timestamp);')!
db.exec('CREATE TRIGGER INSERT_visits AFTER INSERT ON visits BEGIN
UPDATE visits SET created_at = datetime("now", "localtime") WHERE rowid = new.rowid ;
END')
END')!
db.close()!
vweb.run(&App{}, 19123)
}