MySQL Non classé Programmation

Golang and MySQL or MariaDB

MySQL is a popular open-source relational database management system (RDBMS) that is widely used to store and manage data for a wide variety of applications. It is known for its reliability, flexibility, and performance, making it a good choice for many developers.

Golang, also known as Go, is a programming language developed by Google in 2009. It is known for its simplicity, concurrency support, and strong support for networking.

One of the strengths of Go is its ability to work with databases, including MySQL. In this article, we will look at how to connect to and interact with a MySQL database from a Go application.

First, you will need to install the MySQL driver for Go. There are several options available, but the most popular one is called “Go MySQL Driver” (github.com/go-sql-driver/mysql). You can install it using the following command:

go get -u github.com/go-sql-driver/mysql

Next, you will need to import the driver and the standard “database/sql” package in your Go code:

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

To connect to the MySQL database, you will need to provide the driver with a connection string that includes the following information:

  • username and password
  • hostname and port number
  • database name

Here is an example of how to create a connection to a MySQL database using the connection string:

db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/database_name")
if err != nil {
    panic(err.Error())
}
defer db.Close()

Once you have established a connection to the database, you can perform various operations such as executing SQL queries and statements. Here is an example of how to execute a SELECT statement and iterate over the resulting rows:

rows, err := db.Query("SELECT id, name FROM users")
if err != nil {
    panic(err.Error())
}
defer rows.Close()

for rows.Next() {
    var id int
    var name string
    err := rows.Scan(&id, &name)
    if err != nil {
        panic(err.Error())
    }
    fmt.Println(id, name)
}

You can also use prepared statements to execute the same query multiple times with different parameter values. This can be more efficient than executing the same query multiple times with different values, as the query is compiled and optimized by the database server.

stmt, err := db.Prepare("SELECT id, name FROM users WHERE name = ?")
if err != nil {
    panic(err.Error())
}
defer stmt.Close()

var name string
err = stmt.QueryRow("John").Scan(&id, &name)
if err != nil {
    panic(err.Error())
}
fmt.Println(id, name)

In conclusion, MySQL and Go are a powerful combination for building scalable and reliable applications. By using the Go MySQL driver and the standard “database/sql” package, you can easily connect to and interact with a MySQL database from a Go application.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.