【问题标题】:One file two different outputs - Windows Server 2012一个文件两个不同的输出 - Windows Server 2012
【发布时间】:2017-07-14 21:59:33
【问题描述】:

我的程序读入一个 sql 文件并对数据库执行操作。

我昨天通过记事本编辑了服务器上的一个sql文件。

我今天再次通过记事本对同一个文件进行了一次更改。

当程序读入文件时,我对sql所做的更改不存在。

将 sql 内容打印到控制台表明二进制文件正在读取昨天的版本。

这里有什么黑魔法?

删除文件不起作用。

如果我再次创建它,Date created 时间戳来自 1 个月前。 Date modified 是昨天的。

在记事本、写字板中打开文件,任何你能想到的文本阅读器都会显示正确的内容。

二进制读取昨天的版本。

这是二进制文件读取文件的方式

file, err := ioutil.ReadFile("appointment.sql")
if err != nil {
    log.Fatal(err)
}

程序是在 mac 上为 windows 交叉编译的。

Sql文件最初是在mac上通过vim编写的,然后上传到服务器。

编辑:我在建议调试后包含方法中的代码。

func (r *Icar) ReadAppointments(qCfg dms.QueryConfig) []dms.Appointment {
    // r.conn contains the db connection

    /*DEBUGGING*/
    name := "appointment.sql"
    fmt.Printf("%q\n", name)
    path, err := filepath.Abs(name)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%q\n", path) //correct path

    file, err := ioutil.ReadFile("appointment.sql")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%q\n", file) //correct output
    /*END*/

    appointmentQuery := string(file)

    fmt.Println(appointmentQuery) //correct output

    appointmentQuery = strings.Replace(appointmentQuery, "@", qCfg.QueryLocationID, -1)

    fmt.Println(appointmentQuery) //correct output

    rows, err := r.conn.Query(appointmentQuery)
    if err != nil {
        fmt.Println(appointmentQuery) //wrong output. output contains edits from a previous version
        log.Fatal("Error reading from the database: %s", err.Error())
    }

    appointments := []dms.Appointment{}

    var (
        ExternalID,
        WONumber,
        CustomerWaiting interface{}
    )

    for rows.Next() {
        appointment := dms.Appointment{}

        err = rows.Scan(&ExternalID, &WONumber, &appointment.AppointmentDate, &CustomerWaiting)
        if err != nil {
            fmt.Println(appointmentQuery)
            log.Fatal(err)
        }

        toStr := []interface{}{ExternalID, WONumber}
        toInt := []interface{}{CustomerWaiting}

        convertedString := d.ConvertToStr(toStr)
        convertedInt := d.ConvertToInt(toInt)

        appointment.ExternalID = convertedString[0]
        appointment.WONumber = convertedString[1]
        appointment.CustomerWaiting = convertedInt[0]

        appointments = append(appointments, appointment)
    }

    err = rows.Close()

    return appointments
}

我在主函数的延迟语句中关闭了数据库连接。

这里是构造函数供参考

func New(config QueryConfig) (*Icar, func()) {

    db, err := sql.Open("odbc", config.Connection)
    if err != nil {
        log.Fatal("The database doesn't open correctly:\n", err.Error())
    }

    icar := &Icar{
        conn: db,
    }

    return icar, func() {
        icar.conn.Close()
    }
}

【问题讨论】:

    标签: windows go filesystems


    【解决方案1】:

    基本调试表示检查您的输入和输出。您可能正在查看不同的文件。显然,“appointment.sql”在文件系统中不一定是唯一的。例如,这会给您带来预期的结果吗?

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "log"
        "path/filepath"
    )
    
    func main() {
        name := "appointment.sql"
        fmt.Printf("%q\n", name)
        path, err := filepath.Abs(name)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%q\n", path)
        file, err := ioutil.ReadFile(name)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%q\n", file)
    }
    

    输出:

    "appointment.sql"
    "C:\\Users\\peter\\gopath\\src\\so\\appointment.sql"
    "SELECT * FROM appointments;\n"
    

    【讨论】:

    • 感谢调试提示。输出是我所期望的,我现在更加困惑。我用一些代码编辑了我的帖子。如果您能对手头的问题有所了解,我们将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多