英文:
Golang exec.Command Output blank(no error)
问题
我正在尝试在Go中运行一个sqlite3命令,进行一个简单的查询(我完全理解从Go中访问sqlite还有其他方法,但这不是问题所在)。
func sqliteTest(){
cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "'select * from testTable;'")
fmt.Println("Command String:", cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error Accessing Database:", err.Error(), string(out))
return
}
fmt.Println("Result Length:", len(out))
fmt.Println("Result:", string(out))
}
运行这段代码会得到以下输出:
Command String: /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
Result Length: 0
Result:
Process finished with exit code 0
然而,如果我手动运行相同的命令:
$ /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
testName|testValue
在Go中运行命令时,我确实遇到了许多问题,但通常会生成错误,但这次既没有错误,stdout和stderr的结果似乎都为空。
编辑:
单引号和双引号没有改变任何结果。
cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "\"select * from testTable;\"")
仍然返回空输出,但从命令行中按预期工作。
英文:
I am attempting to run a sqlite3 command from within Go, to do a simple query (I completly understand there are other ways to access sqlite from go, but thats not the issue here)
func sqliteTest(){
cmd:=exec.Command("/usr/bin/sqlite3", " /home/chadg/Downloads/testDb.db 'select * from testTable;'")
fmt.Println("Command String:",cmd.String())
out,err:=cmd.CombinedOutput()
if err!=nil{
fmt.Println("Error Accessing Database:",err.Error(),string(out))
return
}
fmt.Println("Result Length:",len(out))
fmt.Println("Result:",string(out))
}
Running this gives me this output:
Command String: /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
Result Length: 0
Result:
Process finished with exit code 0
However if I run the same command by hand:
$ /usr/bin/sqlite3 /home/chadg/Downloads/testDb.db 'select * from testTable;'
testName|testValue
Have certainly run into many issues when running commands from within go, but there is always an error generated, but this time, there is no error, but the result from stdout and stderr seems to both be empty.
EDIT:
Single quotes vs double quotes doesn't change anything
cmd:=exec.Command("/usr/bin/sqlite3", " /home/chadg/Downloads/testDb.db \"select * from testTable;\"")
still returns an empty output, but works as expected from the command line.
答案1
得分: 2
exec.Command
中的参数应该作为单独的字符串传递。
cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "select * from testTable;")
至于你观察到的行为:
当以非交互方式启动sqlite3
并传入一个参数时,它将把该参数作为数据库文件名,并从stdin
读取查询语句,但是cmd.CombinedOutput()
会关闭stdin
,导致输出为空。
英文:
Arguments in exec.Command
should be passed as separate strings.
cmd := exec.Command("/usr/bin/sqlite3", "/home/chadg/Downloads/testDb.db", "select * from testTable;")
As for the behavior you're observing:
When sqlite3
is started non-interactively with 1 argument, it treats that as the database file name and reads the query from stdin
, but cmd.CombinedOutput()
closes stdin
, resulting in an empty output.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论