zoukankan      html  css  js  c++  java
  • golang查询数据遇到空值问题

    golang在查询数据库的时候遇到有空值的列,那么后面的字段提取就会失败,针对这种情况可以有2个办法解决:
    1.限制数据库不为空或给个默认值,或是在select查询的时候对空值进行处理,使其查出来的数据不为空
    2.采用sql.nullstring进行处理

    下面的例子就是sql.sqlstring处理:

        package main
    
        import (
            _ "github.com/go-sql-driver/mysql"
            "fmt"
            "database/sql"
        )
    
        func nulldel(){
            var err error
            connect := "dmladmin:dmladmin@tcp(192.168.1.113:3306)/db_admin"
            db, err := sql.Open("mysql", connect)
            if err != nil{
                fmt.Println("connect mysql failed, address = " + connect, err)
            } else{
                sqlContent := `select
                                    t.statedate,
                                    t.table_schema,
                                    t.table_name,
                      t1.table_comment,
                      t.table_rows,
                                    t.lm_table_rows,
                                    case when t.lm_table_rows > 0 then
                                    round((t.table_rows - t.lm_table_rows)*100/t.lm_table_rows,2)
                                    else 0 end growth_rate,
                                    t.gather_interval
                                from tb_table_grouth_stat t left join information_schema.tables t1
                                on t.table_schema = t1.table_schema
                         and t.table_name = t1.table_name
                              where t.table_schema not in ('db_admin') and statedate = (select max(statedate) from tb_table_grouth_stat)
             order by 7 desc limit 10`
                rows, err := db.Query(sqlContent)
                if err != nil{
                    fmt.Println(err)
                } else{
                    for rows.Next() {
                        statedate := sql.NullString{String:"", Valid:false}
                        table_schema := sql.NullString{String:"", Valid:false}
                        table_name := sql.NullString{String:"", Valid:false}
                        table_comment := sql.NullString{String:"", Valid:false}
                        table_rows := sql.NullString{String:"", Valid:false}
                        lm_table_rows := sql.NullString{String:"", Valid:false}
                        growth_rate := sql.NullString{String:"", Valid:false}
                        gather_interval := sql.NullString{String:"", Valid:false}
                        rows.Scan(&statedate, &table_schema, &table_name,&table_comment,&table_rows,&lm_table_rows,&growth_rate,&gather_interval)
                        if ! table_comment.Valid {
                            table_comment.String="未知"
                        }
                        fmt.Println(statedate, table_schema, table_name,table_comment,table_rows,lm_table_rows,growth_rate,gather_interval)
                        fmt.Println(statedate.String, table_schema.String, table_name.String,table_comment.String,table_rows.String,lm_table_rows.String,growth_rate.String,gather_interval.String)
                    }
                }
            }
        }
        func main() {
            nulldel()
        }
  • 相关阅读:
    php包含(include/require)文件时的作用域
    Android使用DrawerLayout仿qq6.6版本侧滑效果
    Android几种强大的下拉刷新库
    Android 捕捉app系统中未处理的异常
    Android中内存泄露与如何有效避免OOM总结
    Android进程间的通信之Messenger
    Android中Parcelable接口用法
    《完美解决系列》Android5.0以上 Implicit intents with startService are not safe
    Android高效加载大图、多图解决方案,有效避免程序OOM
    使用真机导致Androidstudio打印不出log
  • 原文地址:https://www.cnblogs.com/hxlasky/p/11777425.html
Copyright © 2011-2022 走看看