Home > Article > Backend Development > golang mysql query
Golang is an increasingly popular programming language that excels at handling large amounts of data and high concurrency performance. At the same time, MySQL, as a popular relational database system, is very compatible with Golang. In this article, we will explore how to execute MySQL queries in Golang.
Before using Golang to query MySQL, you need to install the MySQL driver first. The most commonly used MySQL driver in Golang is "mysql", which can be installed through the following command:
go get -u github.com/go-sql-driver/mysql
This command will install the mysql driver in the Golang environment.
Before performing MySQL query, you need to establish a connection with the database first. The following code can be used to establish a connection to the MySQL database:
import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/database") if err != nil { panic(err.Error()) } defer db.Close() }
In the above code, we use the "sql.Open" function to open the MySQL database from Golang with the following parameters:
The second parameter: Specify the connection information of the MySQL database, the format is "username:password@tcp(host :port)/database_name"
After executing the query, you need to close the connection through the "db.Close()" statement.
After establishing the connection, you can use Golang to execute MySQL query. The following is a simple sample code to execute a SELECT query statement:
rows, err := db.Query("SELECT name, age FROM users WHERE age > ?", 20) if err != nil { panic(err.Error()) } defer rows.Close() for rows.Next() { var name string var age int err = rows.Scan(&name, &age) if err != nil { panic(err.Error()) } fmt.Println(name, age) }
In Golang, performing INSERT, UPDATE, DELETE and other operations is basically the same as performing SELECT operations. The following is a sample code:
result, err := db.Exec("INSERT INTO users (name, age) VALUES (?, ?)", "John", 25) if err != nil { panic(err.Error()) } lastInsertId, _ := result.LastInsertId() rowsAffected, _ := result.RowsAffected() fmt.Printf("LastInsertId: %d, RowsAffected: %d ", lastInsertId, rowsAffected)
In Golang , you can use prepared statements to execute MySQL queries. Prepared statements can improve query performance because the MySQL server can cache the same query statements when executing multiple identical query statements, reducing the overhead of compiling SQL statements. The following is an example of using prepared statements:
stmt, err := db.Prepare("SELECT name, age FROM users WHERE age > ?") if err != nil { panic(err.Error()) } defer stmt.Close() rows, err := stmt.Query(20) if err != nil { panic(err.Error()) } defer rows.Close() for rows.Next() { var name string var age int err = rows.Scan(&name, &age) if err != nil { panic(err.Error()) } fmt.Println(name, age) }
Security is very important when making MySQL queries. Golang provides some best practices for using MySQL queries, including:
These best practices can reduce the risk of attacks such as SQL injection.
Conclusion
In this article, we explored how to execute MySQL queries in Golang. After understanding the basic connection methods and syntax, you can use Golang to query the MySQL database and use best practices to ensure the security of the query. When using Golang for MySQL queries, always keep security issues in mind.
The above is the detailed content of golang mysql query. For more information, please follow other related articles on the PHP Chinese website!