现在的位置: 首页 > 综合 > 正文

解析文件,生成指定的SQL

2017年12月21日 ⁄ 综合 ⁄ 共 943字 ⁄ 字号 评论关闭

package main
import (
    //"fmt"
    "os"
    "bufio"
    "io"
    "strings"
)
func addquotation(r string) string {
    return "'" + r + "'"
}
func main() {
    f, err := os.Open("c:\\aaa.txt") //打开文件
    defer f.Close() //打开文件出错处理
    userFile := "out.txt"
    fout, err := os.Create(userFile)
    defer fout.Close()
    if nil == err {
        buff := bufio.NewReader(f) //读入缓存
        for {
            line, err := buff.ReadString('\n') //以'\n'为结束符读入一行
            if err != nil || io.EOF == err {
                break
            }
            field := strings.Split(line, ",")
            sql := "insert into tservicefare (d_date, c_fundname, c_fundcode, f_servicefare,f_managefare, c_flag) values (" +
                addquotation(field[0]) + ", " +
                addquotation("新华阿里一号") + ", " +
                addquotation("000610") + ", " +
                strings.Replace(field[1], "\r\n", "", -1) + ", " +
                "0" + ", " +
                addquotation("0") +
                ");"
            //fmt.Println(sql)
            fout.WriteString(sql + "\n")
        }
    }
}

抱歉!评论已关闭.