In this source code example, we show how to update data into the MySQL database table in Golang with an example.
All Golang source code examples at https://www.sourcecodeexamples.net/p/golang-source-code-examples.html
Required package
To connect to MySQL we need a driver. Here is the driver that we are going to use.
To install it into GOPATH we simply run this command:
G:\GoLang\examples>go get -u github.com/go-sql-driver/mysql
Database Set up
Let's use below SQL statement to create a database in the MySQL server:
create database demo
After creating the database, use the below SQL script to create a students table in the database:
CREATE TABLE `students` (
`id` bigint NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
Insert Data
Let's use the below example to insert data into the students table and then we will be able to select data from a database.
Golang - MySQL Insert Example
Golang - MySQL Update Example
In this example, we will update the first record in the students table.
Let's create a file named "go_example.go" and add the following content to it:
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Student struct {
Id int
Email string
First_Name string
Last_Name string
}
func main() {
db, e := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/demo")
ErrorCheck(e)
// close database after all work is done
defer db.Close()
PingDB(db)
//Update db
stmt, e := db.Prepare("update students set First_Name=? where id=?")
ErrorCheck(e)
// execute
res, e := stmt.Exec("Ramesh", "1")
ErrorCheck(e)
a, e := res.RowsAffected()
ErrorCheck(e)
fmt.Println(a)
}
func ErrorCheck(err error) {
if err != nil {
panic(err.Error())
}
}
func PingDB(db *sql.DB) {
err := db.Ping()
ErrorCheck(err)
}
Output:
>G:\GoLang\examples>go run go_example.go
1
Related Source Code Examples
Related Go (Golang) Source Code Examples:
Go Tutorial - Hello World App
Go function simple example
Go function multiple return values
Go anonymous function
Go variadic function
Go recursive function
Go defer function call
Go pass parameters by value
Go function as a parameter
Go custom function types
Go filter slice example
Go closure function example
Go higher-order function
Go Example: Constants
Go Example: Variables
Go Example: Arrays
Go Example: Slices
Go Example: Maps
Go Example: Range
Go Example: Pointers
Go Example: Values
Go Example: Base64 Encoding
Go Example: Time Formatting / Parsing
Go Variable Examples
Go Example: Interface
Go - Operator Examples
Go - Arithmetic Operators
Go - Boolean Operators
Go - Comparison Operators
Go - Bitwise Operators
Go - Read Input From User With Scanf
Go - Read Input From User With NewReader
Go - Read Input From User With NewScanner
How to Get Input From User in Golang
Go - for Loop Example
Go - If/Else Example
Go - Switch/Case Statement Example
How to Concatenate Strings in Go
How to Convert String to Int in Golang
How to Convert String to Array in Golang
How to Convert String to Int64 in Golang
How to Convert Int to String in Golang
Go - Array Example
Go - Array Iteration Example
Go - Multidimensional Array Example
How to Iterate Over Arrays in Go Using Range
How to Pass Arrays to Functions in Go
How to Copy and Clone Arrays in Go
How to Convert Array to Slice in Golang
How to Sort Array in Golang
How to Implement Custom Sort on Arrays in Go
How to Open and Close Files in Go
How to Read from a File in Go
How to Write to a File in Go
How to Check if a File Exists in Go
How to Delete a File in Go
How to Copy a File in Go
How to Move or Rename a File in Go
How to Change File Permissions in Go
How to Create a Directory in Go
How to List Files in a Directory in Go
How to Recursively Traverse Directories in Go
Go - read file into string
Go - read file line by line
Go - write to file with File.WriteString
Go - write to file with ioutil.WriteFile
How to read and write a file in GoLang
Go - create directory example
How to check if a directory exists in Golang
Go - create, rename and delete directory examples
Go - Create Slice Example
Go - Slice Length and Capacity Example
Go - Slice Iteration Example
Go - Slice Append Example
Go - Slice Copy Example
Go - Slice Remove Element Example
Go - Slice Sorting Example
How to Delete Elements from a Slice in Go
How to Sort Slice in Golang
Go - Multidimensional Slices Example
How to Convert an Array to a Slice in Go
How to Reverse a Slice in Go
How to Concatenate Multiple Slices in Go
How to Check if Slice Is Empty Golang
Go - Custom Slice Types Example
How to Declare and Initialize a Map in Go
How to Add or Update Elements in a Map in Go
How to Retrieve Values from a Map in Go
How to Check if Key Exists in Map Golang
How to Iterate Over a Map in Go
How to Sort a Map by Key in Go
How to Sort a Map by Value in Go
How to Pass a Map to a Function in Go
How to Copy a Map in Go
How to Create Nested Maps in Go
How to Compare Maps in Go
How to Convert a Map into a Slice in Go
Go - Map Example
Go - Map loop example
Go - Map check if value exists
Go - Map remove element example
Go Structs Example
Go - anonymous struct example
Go - nested structs example
Go - struct functions fields example
Go - struct constructor example
Golang - MySQL Delete Example
Golang - MySQL Update Example
Golang - MySQL Select Example
Golang - MySQL Insert Example
How to Convert String to JSON in Golang
How to Convert Struct to JSON in Golang
How to Convert Map to JSON in Golang
How to Convert JSON to Map in Golang
How to Convert XML to JSON in Golang
Golang Interpolation Search Algorithm
Golang Recursion Algorithm
Golang Binary Search Algorithm
Golang Linear Search Algorithm
Golang Quick Sort Algorithm
Golang Merge Sort Algorithm
Golang Shell Sort Algorithm
Golang Insertion Sort Algorithm
Golang Selection Sort Algorithm
Golang AVL Tree with Example
Golang Binary Search Tree with Example
Design Patterns in Go (Golang)
Data Structures and Algorithms in Go
Go
MySQL
Source Code Examples
Comments
Post a Comment