Integrating MySQL Workbench with a Golang Application for CRUD Operations

Abhinav
5 min readSep 10, 2023

--

MySQL, a powerful open-source relational database management system, and Golang, a performant and efficient programming language, can be seamlessly integrated to create robust applications. In this article, we’ll guide you through integrating MySQL Workbench with a Golang application to perform CRUD (Create, Read, Update, Delete) operations on a database. We’ll break down each step in detail with accompanying code examples.

Prerequisites

Before we begin, ensure that you have the following prerequisites installed on your system:

  1. MySQL Workbench: Download and install MySQL Workbench from the official website here.
  2. Golang (Go): Install Golang on your machine by following the official installation guide here.
  3. MySQL Server: Ensure that MySQL Server is installed and running on your system. You’ll also need a MySQL username and password to connect to the database.
  4. MySQL Driver for Go: Install the MySQL driver for Go using the following command:
go get -u github.com/go-sql-driver/mysql

Setting up the MySQL Database

  1. Create a MySQL Database: Open MySQL Workbench, create a new database, and name it “sampledb” for this example.
  2. Create a Table: Inside the “sampledb” database, create a new table called “users” with columns for “id,” “name,” and “email.”
  3. Populate the Table: Add some sample data to the “users” table.

Writing the Golang Application

Now, let’s create a Golang application that connects to the MySQL database and performs CRUD operations. We will break down each operation into separate functions for clarity.

// main.go
package main

import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// Database connection parameters
username := "your_username"
password := "your_password"
host := "localhost"
port := "3306"
databaseName := "sampledb"
// Establish a database connection
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, host, port, databaseName))
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Perform CRUD operations
// 1. Create
createUser(db, "John Doe", "john@example.com")
// 2. Read
users := readUsers(db)
fmt.Println("Users:")
for _, user := range users {
fmt.Printf("ID: %d, Name: %s, Email: %s\n", user.id, user.name, user.email)
}
// 3. Update
updateUser(db, 1, "Updated John Doe", "updated_john@example.com")
// 4. Delete
deleteUser(db, 1)
}
// User struct to represent a user
type User struct {
id int
name string
email string
}
// Create a new user
func createUser(db *sql.DB, name, email string) {
_, err := db.Exec("INSERT INTO users (name, email) VALUES (?, ?)", name, email)
if err != nil {
log.Fatal(err)
}
fmt.Println("User created successfully!")
}
// Read users from the database
func readUsers(db *sql.DB) []User {
rows, err := db.Query("SELECT id, name, email FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var users []User
for rows.Next() {
var user User
err := rows.Scan(&user.id, &user.name, &user.email)
if err != nil {
log.Fatal(err)
}
users = append(users, user)
}
return users
}
// Update a user's information
func updateUser(db *sql.DB, id int, name, email string) {
_, err := db.Exec("UPDATE users SET name = ?, email = ? WHERE id = ?", name, email, id)
if err != nil {
log.Fatal(err)
}
fmt.Println("User updated successfully!")
}
// Delete a user from the database
func deleteUser(db *sql.DB, id int) {
_, err := db.Exec("DELETE FROM users WHERE id = ?", id)
if err != nil {
log.Fatal(err)
}
fmt.Println("User deleted successfully!")
}

In this application:

  • We define a main function that orchestrates the database connection and CRUD operations.
  • The CRUD operations (Create, Read, Update, Delete) are each encapsulated in separate functions (createUser, readUsers, updateUser, and deleteUser) for better code organization and maintainability.
  • The User struct represents the user entity with fields for ID, name, and email.

Now, let’s dive into the details of each CRUD operation:

1. Create

The createUser function takes a database connection (db), a name, and an email as parameters. It executes an SQL INSERT statement to add a new user to the "users" table in the database. If successful, it prints a confirmation message.

2. Read

The readUsers function queries the database to retrieve all users from the "users" table. It returns a slice of User structs, each representing a user with an ID, name, and email. It then prints the retrieved user data.

3. Update

The updateUser function takes a database connection (db), a user ID, a new name, and a new email as parameters. It executes an SQL UPDATE statement to modify the user's name and email based on the provided ID. If successful, it prints a confirmation message.

4. Delete

The deleteUser function takes a database connection (db) and a user ID as parameters. It executes an SQL DELETE statement to remove a user from the "users" table based on the provided ID. If successful, it prints a confirmation message.

By following this structured approach, your code becomes modular and easier to maintain. You can call these functions separately to perform specific database operations as needed.

Running the Application

  1. Save the code to a file named main.go.
  2. Open a terminal and navigate to the directory where main.go is located.
  3. Run the application using the following command:
go run main.go

You should see the application connecting to the MySQL database and performing CRUD operations on the “users” table.

Conclusion

In conclusion, the integration of MySQL Workbench with a Golang application for CRUD operations offers a powerful combination of robust database management and efficient application development. By following the steps outlined in this article, you’ve gained insights into how to seamlessly connect and interact with a MySQL database using Golang.

We began by setting up essential prerequisites, including MySQL Workbench, Golang, the MySQL server, and the MySQL driver for Go. Once these prerequisites were in place, we proceeded to create a Golang application capable of performing CRUD operations on a MySQL database.

Breaking down each CRUD operation into separate functions — Create, Read, Update, and Delete — allowed for a more organized and maintainable codebase. The main function orchestrated the database connection and executed these operations, showcasing the modularity and flexibility of the Golang language.

This integration enables developers to build data-driven applications efficiently, ensuring data integrity and facilitating seamless interaction with the database. Whether you’re building a web application, API, or any other software that relies on database operations, the principles demonstrated in this article serve as a solid foundation.

As you continue to explore the world of Golang and MySQL, you’ll discover countless opportunities to create robust and scalable applications that leverage the power of relational databases. Whether it’s handling user data, managing inventory, or analyzing business metrics, the fusion of Golang and MySQL Workbench provides the tools you need to bring your data-centric ideas to life.

--

--

Abhinav

Hi, I'm Abhinav, a software engineer with a passion for continuous learning and exploring new technologies.