Golang: Store Query Result in a Map

目录

  • [1. Golang: Store Query Result in a Map](#1. Golang: Store Query Result in a Map)
    • [1.1. Using Structs](#1.1. Using Structs)
    • [1.2. Using Maps](#1.2. Using Maps)

1. Golang: Store Query Result in a Map

注意: 使用这个可能会造成列名和列值乱串的现象,解决这个可以使用 AS 语法:

sql 复制代码
SELECT 
  TENANT_ID AS TENANT_ID,
  SVR_IP AS SVR_IP,
  SVR_PORT AS SVR_PORT,
  PLAN_ID AS PLAN_ID,
  SQL_ID AS SQL_ID,
  TYPE AS TYPE,
  DB_ID AS DB_ID,
  STATEMENT AS STATEMENT,
  PLAN_HASH AS PLAN_HASH,
  LAST_ACTIVE_TIME AS LAST_ACTIVE_TIME,
  ELAPSED_TIME AS ELAPSED_TIME
FROM GV$PLAN_CACHE_PLAN_STAT
WHERE LAST_ACTIVE_TIME > '%s' AND LAST_ACTIVE_TIME <= '%s' AND ELAPSED_TIME > %d

Converting the results of a SQL query to a struct in Go is trivial, but sometimes you don't know ahead of time exactly what columns and types you're going to be retrieving, and a map may be better suited for storing the results.

1.1. Using Structs

First, here's the standard way we can convert results to a struct:

go 复制代码
rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    s := myStruct{}
    if err := rows.Scan(&s); err != nil {
        return err
    }
    
    // Do something with 's'
}

Easy enough, but storing your query result in a map is a bit trickier.

1.2. Using Maps

Let's say for example you're working with a user's database where you don't know the schema ahead of time. You can't write a struct to store the results, because you don't know what columns and data types you're going to be retrieving. What we want in this case is a map[string]interface{} where the key is the column name and the value could be any data type.

You might assume you can do the following:

go 复制代码
rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
for rows.Next() {
    m := make(map[string]interface{})
    
    // This WON'T WORK
    if err := rows.Scan(&m); err != nil {
        // ERROR: sql: expected X destination arguments in Scan, not 1
    }
}

Basically the SQL package is thinking that you expect a single column to be returned and for it to be a map[string]interface{} compatible type, which isn't what we we're trying to do.

Instead what we have to do in order to make this work is the following:

go 复制代码
rows, _ := db.Query("SELECT ...") // Note: Ignoring errors for brevity
cols, _ := rows.Columns()

for rows.Next() {
    // Create a slice of interface{}'s to represent each column,
    // and a second slice to contain pointers to each item in the columns slice.
    columns := make([]interface{}, len(cols))
    columnPointers := make([]interface{}, len(cols))
    for i, _ := range columns {
        columnPointers[i] = &columns[i]
    }
    
    // Scan the result into the column pointers...
    if err := rows.Scan(columnPointers...); err != nil {
        return err
    }

    // Create our map, and retrieve the value for each column from the pointers slice,
    // storing it in the map with the name of the column as the key.
    m := make(map[string]interface{})
    for i, colName := range cols {
        val := columnPointers[i].(*interface{})
        m[colName] = *val
    }
    
    // Outputs: map[columnName:value columnName2:value2 columnName3:value3 ...] 
    fmt.Print(m)
}

So how does this work? Well first we query and get our rows as usual, but this time we use rows.Columns() to get a reference to all column names in the result.

Then for each row, we create a slice of interface{}'s called columns who's length matches the number of columns. Next we create a second slice with the same length called columnPointers, but this time we iterate over each element in columns and store a pointer to the interface{} element in our columnPointers slice. This is necessary because the sql package requires pointers when scanning. So now we have two slices, one of interface{}s and one of pointers to the interface{}s.

Now we can scan the row into the slice of interface{} pointers (ie. columnPointers).

Finally, we create our map[string]interface{}, and iterate over the column names. For each column name (colName), we deference the interface{} pointer at the current loop index from the columnPointers slice, which references the value in the columns slice. We take this dereferenced value and store it in the map as the value, with the key being the column name.

Now we can use the map however we need, essentially allowing us to perform queries dynamically, without requiring knowledge of the schema we're going to be querying when we write our code.

This may seem a little confusing at first, especially if you have no prior experience with pointers. If so, I'd recommend reading up on Pointers and trying the code out, inserting some debug logging to fully understand what's happening during each step of the process.

相关推荐
小白教程1 小时前
MySQL数据库的安全性防护
数据库·mysql
Lion Long1 小时前
CodeBuddy 中国版 Cursor 实战:Redis+MySQL双引擎驱动〈王者荣耀〉战区排行榜
数据库·redis·mysql·缓存·腾讯云·codebuddy首席试玩官·codebuddy
apcipot_rain4 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
辛一一6 小时前
neo4j图数据库基本概念和向量使用
数据库·neo4j
巨龙之路7 小时前
什么是时序数据库?
数据库·时序数据库
蔡蓝7 小时前
binlog日志以及MySQL的数据同步
数据库·mysql
是店小二呀8 小时前
【金仓数据库征文】金融行业中的国产化数据库替代应用实践
数据库·金融·数据库平替用金仓·金仓数据库2025征文
炒空心菜菜9 小时前
SparkSQL 连接 MySQL 并添加新数据:实战指南
大数据·开发语言·数据库·后端·mysql·spark
专注于大数据技术栈9 小时前
Mac上安装Mysql的详细步骤及配置
mysql
多多*9 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle