Golang database/sql.Tx.Exec and Rollback functions examples
package database/sql
Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
Rollback aborts the transaction.
Golang database/sql.Tx.Exec and Rollback functions usage examples
Example 1:
result, err := tx.Exec(`INSERT INTO order_request (member_id) VALUES (?)`, memberId)
if err != nil {
log.Print(err.Error())
}
orderId, err := result.LastInsertId()
Example 2:
_, err = tx.Exec("DELETE FROM "+SqlTableName+" WHERE id = $1", id)
if err != nil {
tx.Rollback()
return err
}
References :
Advertisement
Something interesting
Tutorials
+7.1k Restart Apache or Nginx web server without password prompt
+8.1k Golang : Check from web if Go application is running or not
+6.7k Golang : Humanize and Titleize functions
+26.7k Golang : How to check if a connection to database is still alive ?
+7.9k Golang : Ways to recover memory during run time.
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+35.1k Golang : Upload and download file to/from AWS S3
+15.3k Golang : Get all local users and print out their home directory, description and group id
+15.6k Golang : Validate hostname
+8.1k Golang : How To Use Panic and Recover
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name