1 回答

TA貢獻1802條經(jīng)驗 獲得超5個贊
該exec.Command函數(shù)只創(chuàng)建命令,不執(zhí)行它。
要從 獲取輸出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
注意:對于數(shù)據(jù)庫交互,exec不是慣用代碼。
SQL 庫允許與數(shù)據(jù)庫直接交互:http : //golang.org/pkg/database/sql/
- 1 回答
- 0 關(guān)注
- 355 瀏覽
添加回答
舉報