一个文件两个不同的输出-Windows Server 2012
我的程序读入sql文件并对数据库执行操作 昨天我通过记事本编辑了服务器上的一个sql文件 今天我在同一个文件上又做了一次更改,还是通过记事本 当程序读取文件时,我对sql所做的更改并不存在 将sql内容打印到控制台显示二进制文件正在读取昨天的版本 这里有什么黑魔法在起作用 删除文件不起作用 如果我再次创建,则创建的日期是1个月前的时间戳<代码>修改日期从昨天开始 在记事本中打开文件,任何你能想到的文本阅读器都会显示正确的内容 二进制读取昨天的版本 这是二进制文件读取文件的方式一个文件两个不同的输出-Windows Server 2012,windows,go,filesystems,Windows,Go,Filesystems,我的程序读入sql文件并对数据库执行操作 昨天我通过记事本编辑了服务器上的一个sql文件 今天我在同一个文件上又做了一次更改,还是通过记事本 当程序读取文件时,我对sql所做的更改并不存在 将sql内容打印到控制台显示二进制文件正在读取昨天的版本 这里有什么黑魔法在起作用 删除文件不起作用 如果我再次创建,则创建的日期是1个月前的时间戳修改日期从昨天开始 在记事本中打开文件,任何你能想到的文本阅读器都会显示正确的内容 二进制读取昨天的版本 这是二进制文件读取文件的方式 file, err :=
file, err := ioutil.ReadFile("appointment.sql")
if err != nil {
log.Fatal(err)
}
该程序是在mac上为windows交叉编译的
Sql文件最初通过vim写入mac,然后上传到服务器
编辑:我在建议的调试之后包含了方法中的代码
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
}
我在主函数中的延迟语句中关闭db连接
这里是构造函数供参考
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()
}
}
基本调试要求检查输入和输出。您可能正在查看不同的文件。显然,“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"
谢谢你的调试提示。结果是我所期望的,我现在更困惑了。我用一些代码编辑了我的帖子。如果您能对当前的问题有所了解,我们将不胜感激。