我的程序读取一个sql文件并在数据库上执行操作。
我昨天通过记事本编辑了服务器上的一个sql文件。
我今天又通过记事本对同一个文件进行了一次更改。
当程序读入文件时,我对 sql 所做的更改不存在。
将 sql 内容打印到控制台显示二进制文件正在读取昨天的版本。
这里有什么黑魔法在起作用?
删除文件不起作用。
如果我再次创建它,创建日期
时间戳是从 1 个月前开始的。 修改日期
是昨天的。
在记事本、写字板中打开文件,任何您能想到的文本阅读器都会显示正确的内容。
二进制读取昨天的版本。
这是二进制文件读取文件的方式
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()
}
}
最佳答案
基本调试说检查你的输入和输出。您可能正在查看不同的文件。显然,“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"
关于windows - 一个文件两个不同的输出 - Windows Server 2012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098660/