Preface
In Go, connecting and operating databases are usually useddatabase/sql
Package, it provides a database abstraction layer and supports multiple database engines, such as MySQL, PostgreSQL, SQLite, etc. Below I will use MySQL as an example to explain in detail how to connect and operate databases using Go language.
1. Install MySQL driver
First, you need to install a MySQL driver to be able to communicate with the MySQL database. Recommended use/go-sql-driver/mysql
, can be installed through the following command:
go get -u /go-sql-driver/mysql
2. Connect to the database
useFunction establishes a connection to the MySQL database. This function accepts two parameters: the database driver name and the connection string. The connection string contains all the information needed to connect to the database, such as username, password, hostname, port and database name.
Sample code:
package main import ( "database/sql" "fmt" "log" _ "/go-sql-driver/mysql" ) func main() { // Define the connection string db, err := ("mysql", "user:password@tcp(127.0.0.1:3306)/dbname") if err != nil { (err) } defer () // Verify the connection err = () if err != nil { (err) } ("Connected to database successfully!") }
3. Execute query
useMethod executes SQL query and obtains
*
Result set. You can use()
Methods traverse the result set and use()
Method scans each row of data into a variable.
Sample code:
package main import ( "database/sql" "fmt" "log" _ "/go-sql-driver/mysql" ) func main() { // Connect to the database db, err := ("mysql", "user:password@tcp(127.0.0.1:3306)/dbname") if err != nil { (err) } defer () // Execute query rows, err := ("SELECT id, name FROM users") if err != nil { (err) } defer () // traverse the result set for () { var id int var name string err := (&id, &name) if err != nil { (err) } ("ID: %d, Name: %s\n", id, name) } // Check whether there are errors during traversal if err := (); err != nil { (err) } }
4. Perform insert, update and delete operations
useMethods execute SQL statements such as insert, update, and delete operations.
Method returns a
Object, you can use it to get the number of affected rows.
Sample code:
package main import ( "database/sql" "fmt" "log" _ "/go-sql-driver/mysql" ) func main() { // Connect to the database db, err := ("mysql", "user:password@tcp(127.0.0.1:3306)/dbname") if err != nil { (err) } defer () // Insert data _, err = ("INSERT INTO users (name, age) VALUES (?, ?)", "Alice", 25) if err != nil { (err) } ("Data inserted successfully!") // Update data _, err = ("UPDATE users SET age = ? WHERE id = ?", 30, 1) if err != nil { (err) } ("Data updated successfully!") // Delete data _, err = ("DELETE FROM users WHERE id = ?", 1) if err != nil { (err) } ("Data deleted successfully!") }
The above are the basic steps for connecting and operating MySQL databases using Go.
This is the introduction to this article about the basic steps of connecting and operating databases using Go language. For more related content on Go connection and operating databases, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!