Golang : Update database with GORM example
A simple tutorial on how to use GORM(Golang Object Relational Map) to connect to a MySQL/MariaDB database and UPDATE data.
Here you go!
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"time"
)
// Activities table SQL :
// id bigint(20) AUTO_INCREMENT
// username varchar(50)
// created_on timestamp
// action char(1)
// description varchar(300)
// visibility char(1)
// NOTE : In the struct, CreatedOn will be translated into created_on in sql level
type Activities struct {
Id int `sql:"AUTO_INCREMENT"`
Username string `sql:"varchar(50);unique"`
CreatedOn time.Time `sql:"timestamp"`
Action string `sql:"type:char(1)"`
Description string `sql:"type:varchar(300)"`
Visibility string `sql:"type:char(1)"`
}
func main() {
dbConn, err := gorm.Open("mysql", "username:password@tcp(x0.xx0.xxx.xxx:3306)/db_name?charset=utf8&parseTime=true")
if err != nil {
fmt.Println(err)
}
dbConn.DB()
dbConn.DB().Ping()
dbConn.DB().SetMaxIdleConns(10)
dbConn.DB().SetMaxOpenConns(100)
activity := Activities{
Username: "testuser",
CreatedOn: time.Now().UTC(),
Description: "Testing",
Visibility: "S",
}
dbConn.Create(&activity)
// use a clean Activities struct for update purpose
act := Activities{}
// Get the last record
dbConn.Last(&act)
fmt.Println("Before update: ", act)
fmt.Println("Created on : ", act.CreatedOn)
currentId := act.Id
fmt.Println("Current ID : ", currentId)
// update the last activity struct
act.Username = "test test test"
act.Description = "This is a test test test description"
act.Visibility = "A"
//dbConn.Save(&act) // update current id
// best practice is to include the id for the update statement
dbConn.Where("id = ?", currentId).Save(&act)
// Get the last record - again
dbConn.Last(&act)
fmt.Println("After update : ", act)
}
Sample output :
Before update: {27570 testuser 2015-07-29 03:29:44 +0000 UTC Testing S}
Created on : 2015-07-29 03:29:44 +0000 UTC
Current ID : 27570
After update : {27570 test test test 2015-07-29 03:29:44 +0000 UTC This is a test test test description A}
References :
https://github.com/jinzhu/gorm
https://www.socketloop.com/tutorials/golang-gorm-read-from-database-example
See also : Golang : GORM read from database example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.8k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+26.2k Golang : Convert(cast) string to uint8 type and back to string
+6.9k Golang : How to call function inside template with template.FuncMap
+32.5k Golang : Regular Expression for alphanumeric and underscore
+4.6k Linux/MacOSX : How to symlink a file?
+10k Golang : Find and replace data in all files recursively
+7.2k Golang : How to iterate a slice without using for loop?
+7.5k Android Studio : AlertDialog to get user attention example
+16.1k Golang : How to extract links from web page ?
+16.2k Golang :Trim white spaces from a string
+8.1k Golang : Metaprogramming example of wrapping a function
+17.7k Golang : Login and logout a user after password verification and redirect example