1

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.

6
  • cmd:=exec.Command("/usr/bin/sqlite3", " /home/chadg/Downloads/testDb.db","'select * from testTable;'") though i am unsure about the single quotes around the sql Commented Aug 26, 2021 at 21:02
  • The single quotes are used from the command line (see where I ran it by hand), I also tried escaping double quotes to see if that changed anything. Same result, runs from the terminal, but gets empty output when run from Go. Commented Aug 26, 2021 at 21:04
  • take care to the leadng space in your path " /home/chadg/Downloads/testDb.db 'select * from testTable;'" Commented Aug 26, 2021 at 21:05
  • 1
    you have to split the two arguments, the path and the sql query. Commented Aug 26, 2021 at 21:07
  • @mh-cbon thank, that was a copy/paste error, removed it and still didnt change anything. Commented Aug 26, 2021 at 21:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep... Thank. I had tried separating them, but I had left the sql argument quoted, and this causes an error. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.