Home  >  Article  >  Backend Development  >  Basic concepts and usage analysis of SQL in Go language

Basic concepts and usage analysis of SQL in Go language

王林
王林Original
2024-03-27 17:30:11850browse

Basic concepts and usage analysis of SQL in Go language

Basic concepts and usage analysis of SQL in Go language

SQL (Structured Query Language) is a language specially used to manage and operate relational databases. In Go language, we usually use SQL to perform database operations, such as querying data, inserting data, updating data, deleting data, etc. This article will introduce the basic concepts and usage of SQL in Go language, with specific code examples.

1. Connect to the database

In the Go language, we can use third-party libraries to connect to the database. Commonly used libraries include database/sql and various database drivers. . First, we need to import the database driver, for example, import the github.com/go-sql-driver/mysql that connects to the MySQL database:

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

Then, we can pass sql. Open function to connect to the database, the sample code is as follows:

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

2. Query data

Generally speaking, we can use the Query function to perform query operations, The sample code is as follows:

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

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

3. Insert data

If you need to insert data, we can use the Exec function. The sample code is as follows:

stmt, err := db.Prepare("INSERT INTO users(name) VALUES(?)")
if err != nil {
    panic(err.Error())
}
defer stmt.Close()

result, err := stmt.Exec("Alice")
if err != nil {
    panic(err.Error())
}

id, _ := result.LastInsertId()
fmt.Println("Inserted ID:", id)

4 .Updating data and deleting data

The operations of updating and deleting data are similar to inserting data. You only need to change the SQL statement to the corresponding UPDATE and DELETE statement. The sample code for updating data is as follows:

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

result, err := stmt.Exec("Bob", 1)
if err != nil {
    panic(err.Error())
}

rowsAffected, _ := result.RowsAffected()
fmt.Println("Rows affected:", rowsAffected)

The sample code for deleting data is as follows:

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

result, err := stmt.Exec(1)
if err != nil {
    panic(err.Error())
}

rowsAffected, _ := result.RowsAffected()
fmt.Println("Rows affected:", rowsAffected)

Through the above sample code, you can see the basic method of using SQL for database operations in the Go language. Of course, this is just the basic usage of SQL in Go language. Practical applications may involve more complex operations, and you need to flexibly use SQL statements to operate the database according to specific circumstances. I hope this article can help you better understand the basic concepts and usage of SQL in Go language.

The above is the detailed content of Basic concepts and usage analysis of SQL in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn