With the continuous development of the Internet, the amount of data continues to increase, and databases, as an important tool for storing data, are also constantly being used. Among them, the MySQL database is widely used due to its advantages of open source, ease of use and superior performance. This article will explore how to perform data query and filtering on MySQL database through Go language.
1. Install Go language and MySQL driver
First, we need to install Go language and MySQL driver in order to connect to the MySQL database and perform data query and filtering in the program. The specific steps are as follows:
1. Install Go language
Visit the official website (https://golang.org/dl/) to download the Go language installation package corresponding to the operating system, and configure the environment variables after installation .
2. Install the MySQL driver
Use the following command on the command line to install the MySQL driver: go get -u github.com/go-sql-driver/mysql
二, Connecting to MySQL database
To connect to the MySQL database in Go language, you need to use the database/sql library in Go language and use the MySQL driver to connect to the database. The code to connect to the MySQL database is as follows:
import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) db, err := sql.Open("mysql", "user:password@tcp(localhost)/dbname") if err != nil { log.Fatal(err) } defer db.Close()
Among them, user
and password
are the MySQL database user name and password, localhost
is the server where the MySQL database is located IP address or domain name, dbname
is the name of the database to be connected.
3. Perform data query and filtering
After connecting to the MySQL database, we can perform data query and filtering through the method provided by the sql.DB library of the Go language. The following are some commonly used data query and filtering methods:
1. Query all data
Use the following statement to query all data in a table named user
:
rows, err := db.Query("SELECT * FROM user")
2. Query with conditions
Assume that there are id
, name
and age## in the
user table #Three fields, we query the information of all users older than 18 years old based on the value of the
age field:
rows, err := db.Query("SELECT id, name FROM user WHERE age > ?", 18)Among them,
? is a placeholder, indicating the following The next 18 will replace it. This statement will query the id and name fields of users older than 18 in the
user table.
user table. You can use the following statement:
rows, err := db.Query("SELECT * FROM user WHERE name LIKE ?", "Zhang%")Among them,
% wildcard means matching any character,
Zhang% means user information with the surname Zhang.
rows, err := db.Query("SELECT id, name FROM user ORDER BY age DESC")This statement will sort the user age from oldest to youngest. 4. Processing query resultsThe query operation returns a
sql.Rows type of result set, and you need to use the
Scan method to row by row Read query results. The following is a sample code to read the query results:
for rows.Next() { var id int var name string var age int err = rows.Scan(&id, &name, &age) if err != nil { log.Fatal(err) } fmt.Printf("ID: %d, Name: %s, Age: %d ", id, name, age) }where
id,
name and
age are three in the table respectively field. Read the data of each row through the
rows.Scan method, and then perform related processing.
The above is the detailed content of How to use Go language to query and filter data in MySQL database. For more information, please follow other related articles on the PHP Chinese website!