1 回答
TA贡献1802条经验 获得超5个赞
该exec.Command函数只创建命令,不执行它。
要从 获取输出tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb"),您需要运行该命令并捕获其输出:
tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")
//capture the output pipe of the command
outputStream := bufio.NewScanner(tablesname.StdoutPipe())
tablesname.Start() //Runs the command in the background
for outputStream.Scan() {
//Default scanner is a line-by-line scan, so this will advance to the next line
ls := exec.Command("mdb-export", "-I", "postgres", "-q", "\'", "myaccessdatabase.mdb", outputStream.Text())
ls.Run() //Blocks until command finishes execution
visible := exec.Command("psql", "-d", "mypsqldatabase", "-U", "postgres", "-w", "-h", "localhost")
visible.Run()
}
tablesname.Wait() //Cleanup
注意:对于数据库交互,exec不是惯用代码。
SQL 库允许与数据库直接交互:http : //golang.org/pkg/database/sql/
- 1 回答
- 0 关注
- 325 浏览
添加回答
举报