2015-09-19 14:17
浏览 359


I have eight Microsoft access databases each one has around 215 tables and I needed to transfered those databases into postgresql so i used mdb-tools and exported the schemes which just one step ;but when it come to exporting tables data into postgres directly into postgresql i have to write this command for every single table:

mdb-export -I postgres -q \' myaccessdatabase.mdb table-name | psql -d mypsqldatabase -U postgres -w -h localhost 

so I have been trying to write a go command program to do as follows: 1. firstly excute a command to list tables name. which will be the arg of the next command. 2. then start for range looop to excute a command that export tanle data and the output of this command is pipe into the next command. 3. this command is psql which will write the output from the previous command ( which is sql insert statment)

package main

import (
func main() {
    // command to Collect tables name and list it to the next command 
          tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")

    // Run the command on each table name and get the output pipe/feed into the psql shell 
    for _, table := range tablesname {
        ls := exec.Command("mdb-export", "-I", "postgres", "-q", "\'", "myaccessdatabase.mdb", table)

        // | psql -d mydatabase -U postgres -w -h localhost command which will write each line from the output of previouse command's 
        visible := exec.Command("psql", "-d", "mypsqldatabase", "-U", "postgres", "-w", "-h", "localhost")


So i have tried to pipe the output into the stdin of the next command and couldn't implement it , meanwhile I am trying with goroutin and channels just been unable to even come with a way to make this into the last command.

Thank you very much in advance.

图片转代码服务由CSDN问答提供 功能建议

我有八个Microsoft Access数据库,每个数据库都有大约215个表,我需要将这些数据库转移到postgresql中,所以我使用了 mdb-tools并导出了仅一步之遥的方案;但是当涉及到将表数据直接导出到postgres到postgresql时,我必须为每个表编写此命令:

   mdb-export -I postgres -q \'myaccessdatabase.mdb表名|  psql -d mypsqldatabase -U postgres -w -h localhost 

所以我一直在尝试编写go命令程序来执行以下操作: 1。 首先执行命令列出表名。 这将是下一个命令的arg。 2。 然后开始进行范围循环以执行导出密文数据的命令,并且此命令的输出通过管道传递到下一个命令。 3。 此命令是psql,它将写入前一个命令的输出(即sql插入语句)

  package main 
“ bufio” 
“ log  “ 
” os“ 
” os / exec“ 
func main(){
表名:= exec.Command(” mdb- 表”,“-1”,“ myaccessdatabase.mdb”)
 //在每个表名上运行命令,并将输出管道/提要输入到_,table:= range tablename {\  n ls:= exec.Command(“ mdb-export”,“ -I”,“ postgres”,“ -q”,“ \'”,“ myaccessdatabase.mdb”,表)
 // |  psql -d mydatabase -U postgres -w -h localhost命令,它将从先前命令
的输出中写出每一行:= exec.Command(“ psql”,“ -d”,“ mypsqldatabase”,“ -U”  ,“ postgres”,“-w”,“-h”,“ localhost”)

所以我 试图将输出传递到下一个命令的stdin中,但无法实现它,与此同时,我正在尝试使用goroutin,通道甚至无法提供将其输入到最后一个命令中的方法。 \ n


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandong2562 2015-09-19 14:48

    The exec.Command function only creates the command, it doesn't execute it.

    To get the output from tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb"), you need to run the command and capture its output:

    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")
    tablesname.Wait()  //Cleanup 

    BEWARE: For database interactions, exec isn't idiomatic code.

    The SQL library allows direct interaction with the database:

    解决 无用
    打赏 举报

相关推荐 更多相似问题