参考以下的代码:
package database
import (
"Forensics_Equipment_Plugin_Manager/logger"
"Forensics_Equipment_Plugin_Manager/model"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
var SqliteConn *gorm.DB
func init() {
InitDb()
}
func InitDb() {
// 1. 获取连接
var err error
//SqliteConn, err = gorm.Open(sqlite.Open(store.SqliteDsn), &gorm.Config{})
//SqliteConn, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
SqliteConn, err = gorm.Open(sqlite.Open("/home/demo/db.db"), &gorm.Config{})
if err != nil {
panic(err)
}
logger.Log.Info("SQLite initializing succeed!")
// 2. 自动迁移
err = SqliteConn.AutoMigrate(
&model.Plugins{},
&model.Group{},
&model.RecordInstruct{},
)
if err != nil {
logger.Log.Error("AutoMigrate: ", err)
return
}
//SqliteConn.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&model.RecordInstruct{})
// 3. 如果为空表添加示例数据
AddDemoTest()
}
// UpdateByInstance 将数据库中的数据更新成指定行。¥
// 未测试
func UpdateByInstance(value any) (rowsAffected int) {
SqliteConn.Updates(value)
rowsAffected = int(SqliteConn.RowsAffected)
return
}
标签:sqlite,cgo,err,Open,Golang,model,SqliteConn,gorm
From: https://www.cnblogs.com/runninginsilence/p/17936713