Home > OS >  Open sqlite db file from a project's subfolder
Open sqlite db file from a project's subfolder

Time:08-07

I have this folder structure:

├── gorm.sqlite // this works
├── main.go
├── simulator
│   ├── data
│   │   ├── gorm.sqlite //this does not work
│   │   └── profiles.go
│   ├── driver
│   │   ├── apis.go
│   │   ├── client.go
│   │   ├── routing-events.go
│   │   └── websocket.go
│   └── router.go

In my simulator/router.go:

func SetupConfig() {
    config := &driver.Config{}
    if db, err := gorm.Open(sqlite.Open("gorm.sqlite"), &gorm.Config{}); err != nil {
        panic(err)
    } else {
        config.Db = db
    }
    //etc
}

While sqlite.Open("gorm.sqlite") works and access the file in the project main folder, sqlite.Open("/simulator/data/gorm.sqlite") does not work! it panics unable to open database file: no such file or directory. How can I access a db file in a sub folder.

CodePudding user response:

Try this way

    db, err := gorm.Open(sqlite.Open("./simulator/data/gorm.sqlite"), &gorm.Config{})
  • Related