doter1995 2013-06-10 17:18
浏览 43
已采纳

golang:如果不将fmt.Printf()置于for循环的末尾,则无法将CSV文件转换为MS SQL

I am using Go to read CSV file and save the records in a MS SQL database using go-odbc. It works great, but I have an issue where some records (about 10 records) do not get stored. This a random problem, sometimes 3 do not get saved, other times 2, etc. The only time where all the records are saved is when I put fmt.Printf(" ") at the end of the for loop. Notice it has to print a blank space it cannot just be fmt.Printf(""). I am not sure what am I do not wrong. Any suggestions is appreciated. Also, no errors are produced, the program terminates normally.

I included the code with the related problem, let me know if you need me to post the entire code.

Go version: go1.1 windows/amd64

for {
    record, err := c.Read()
    if err == io.EOF {
        break
    } else if err != nil {  
        fmt.Printf("Error while reading %s: %s
", filename, err)
    } else {
        //replace the single quote at the beginning and end of string
        re, err := regexp.Compile("^'|'$")
        params := make([]interface{}, 0, numElements)
        valueHolders := make([]string, 0, numElements)
        tmpFields := make([]string, 0, numElements)
        count := 0

        for i:=1;i<=numElements;i++ {  
            tmp := re.ReplaceAllString(record[i],"")

            //insert only non-empty values
            if len(tmp) > 0 {
                params = params[0:count+1]
                params[count] = tmp

                valueHolders = valueHolders[0:count+1]
                valueHolders[count] = "?"

                tmpFields = tmpFields[0:count+1]
                tmpFields[count] = fieldNames[i-1]

                count++
            }
        }

        query := "insert into [l2test].[dbo]."+tablename+" (" + strings.Join(tmpFields, ",") + ") values (" + strings.Join(valueHolders, ",") + ")"
       stmt, err := dest.Prepare(query)

       if stmt == nil {
           fmt.Printf("Error preparing statment: %s
Query: %s
%v

", err, query, params)
       } else {
          stmt.Execute(params...)
          stmt.Close()
       }
   }

   fmt.Printf(" ")
}
  • 写回答

1条回答 默认 最新

  • dongzhuang2030 2013-06-10 20:28
    关注

    When you drop a few elements at the end, it's usually because you dropped them from the end of the input or didn't write/flush/commit/close them on output. For example, on input you may be ignoring the last slice at EOF. I would write your EOF and error tests differently.

    The error handling for your database access needs improvement too. For example, it should check for errors.

    Once we find database access errors, add some diagnostic information, such as the query and the record, to the returned error (err).

    func insertRecord(conn *odbc.Connection, query string, params []interface{}) error {
        stmt, err := conn.Prepare(query)
        defer func() {
            if stmt != nil {
                stmt.Close()
            }
        }()
        if err != nil {
            return err
        }
        err = stmt.Execute(params...)
        if err != nil {
            return err
        }
        return nil
    }
    

    for {
        record, err := c.Read()
        if err != nil {
            if err != io.EOF {
                fmt.Printf("Error while reading %s: %s
    ", filename, err)
                break
            }
            if len(record) == 0 {
                break
            }
        }
    
        // do things with a record
    
        query := "insert into [l2test].[dbo]." + tablename +
            " (" + strings.Join(tmpFields, ",") + ")" +
            " values (" + strings.Join(valueHolders, ",") + ")"
        err = insertRecord(dest, query, params)
        if err != nil {
            err = fmt.Errorf("%s
    %s
    %v
    %s", err, query, params, strings.Join(record, "||"))
            fmt.Println(err)
            continue
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)