Home > Back-end >  Insert data into sqlite db with Go Fiber / Gorm
Insert data into sqlite db with Go Fiber / Gorm

Time:02-10

Hello I'm using Go Fiber together with gorm sqlite. I was wondering if there is a way to pre load data into the database with a sql script?

I know that in Spring Boot it is possible to create a data.sql file to preload data. Is there a same way to accomplish that for go fiber / gorm sqlite?

CodePudding user response:

You could read the sql file and execute the raw query in Gorm, and you could execute this before booting up the server.

path := filepath.Join("path", "to", "script.sql")

c, ioErr := ioutil.ReadFile(path)
if ioErr != nil {
     // handle error.
}

sql := string(c)

// gorm *DB
err := db.Exec(sql).Error
if err != nil {
    // handle error
}
  • Related