简体   繁体   中英

Preload data into sqlite db with Go Fiber / Gorm

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?

Is there a same way to accomplish that for go fiber \/ gorm sqlite?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM