added information about transactions
This commit is contained in:
53
en/05.3.md
53
en/05.3.md
@@ -64,12 +64,12 @@ An example:
|
||||
// query
|
||||
rows, err := db.Query("SELECT * FROM userinfo")
|
||||
checkErr(err)
|
||||
|
||||
var uid int
|
||||
var username string
|
||||
var department string
|
||||
var created time.Time
|
||||
|
||||
for rows.Next() {
|
||||
var uid int
|
||||
var username string
|
||||
var department string
|
||||
var created time.Time
|
||||
err = rows.Scan(&uid, &username, &department, &created)
|
||||
checkErr(err)
|
||||
fmt.Println(uid)
|
||||
@@ -77,7 +77,9 @@ An example:
|
||||
fmt.Println(department)
|
||||
fmt.Println(created)
|
||||
}
|
||||
|
||||
|
||||
rows.Close() //good habit to close
|
||||
|
||||
// delete
|
||||
stmt, err = db.Prepare("delete from userinfo where uid=?")
|
||||
checkErr(err)
|
||||
@@ -102,7 +104,44 @@ An example:
|
||||
|
||||
You may have noticed that the code is almost the same as in the previous section, and that we only changed the name of the registered driver and called `sql.Open` to connect to SQLite in a different way.
|
||||
|
||||
As a final note on this secton, there is a useful SQLite management tool available: [http://sqliteadmin.orbmu2k.de/](http://sqliteadmin.orbmu2k.de/)
|
||||
Note that sometimes you can't use the `for` statement because you don't have more than one row, then you can use the `if` statement
|
||||
|
||||
if rows.Next() {
|
||||
err = rows.Scan(&uid, &username, &department, &created)
|
||||
checkErr(err)
|
||||
fmt.Println(uid)
|
||||
fmt.Println(username)
|
||||
fmt.Println(department)
|
||||
fmt.Println(created)
|
||||
}
|
||||
|
||||
Also you have to do a `rows.Next()`, without using that you can't fetch data in the `Scan` function.
|
||||
|
||||
Transactions
|
||||
===============
|
||||
|
||||
The above example states how you fetch data from the database, but when you want to write a webapplication then it'll not only fetch data from the db but it'll also write data into it, for that, you use transactions becuase the database might get locked if you do not use transactions since there can be multiple go routines which access the database, and you do not want this happening to your web application. Also if you do not use transactions then a lot of things can go wrong with the web app.
|
||||
|
||||
trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
tx, err := database.Begin()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, err = tx.Stmt(trashSQL).Exec(id)
|
||||
if err != nil {
|
||||
fmt.Println("doing rollback")
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
As it is clear from the above block of code, you first prepare a statement, after which you execute it, depending on the output of that execution then you either roll it back or commit it.
|
||||
|
||||
|
||||
As a final note on this section, there is a useful SQLite management tool available: [http://sqliteadmin.orbmu2k.de/](http://sqliteadmin.orbmu2k.de/)
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user