1

I am just starting learning golang and am not sure if my error is conceptual or with the language.

It is peculiar, because the error only occurs, when unit-testing my code. If I "go run" everything works fine. As a sqlite-driver I use mattn/go-sqlite3.

Here is where the problem occurs:

    func dbExec(command *string) {
    db, err := sql.Open("sqlite3", dbPath) // Path and driver are set correcrtly
    defer db.Close()
    if err != nil { //No problem here
        panic(err)
    }
    _, err = db.Exec(*command)
    if err != nil { //Here the errorNo14 "Unable to open the database file" occurs
        panic(err)
    }
 }

So it seems to me, that the database file can be found, but it can't be open because of other limits. I have no concurrency in all of my code. I have only one test-method so far, so even if the testing would be done concurrently, it would so far be only one thread. Maybe someone has an idea! The problem seems to basic, there really is not much more to my code then this.

1 Answer 1

2

sql.Open usually does not perform any database operations, it merely initializes the driver (see sql.Open docs for details). The driver's Open() method is called in the background.

This means that the first error is usually encountered on trying to perform an operation, in your case: the db.Exec.

As for the actual error: if this is in tests it is likely that your path is not correct (eg: if the test database is in the test's sub-directory, the test runner's current directory will be different, I think). Try to switch to absolute paths (or paths computed at runtime).

And a small nit: move defer db.Close() after the first error check.

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

1 Comment

Thank you so much Marc! Your idea was exactly right. My path was relativ. When running the unit-test the path was false, when running main.go, it fit. Super easy, if you know it.

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.