3 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊
這似乎不是在戈?duì)柲分姓_打開(kāi) SQLite 數(shù)據(jù)庫(kù)的方法。
您缺少 SQLite 驅(qū)動(dòng)程序的導(dǎo)入,而不是傳遞字符串“sqlite3”,而應(yīng)該傳遞和指向 .sqlite.Open("sample.db")gorm.Config
請(qǐng)參閱 https://gorm.io/docs/connecting_to_the_database.html#SQLite
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// github.com/mattn/go-sqlite3
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
func init在建立數(shù)據(jù)庫(kù)連接之前執(zhí)行,gorm 無(wú)法遷移,并且在此處引發(fā)恐慌。
試試這個(gè)代碼
func main(){
gormDB, err = gorm.Open("sqlite3", "sample.db")
if err != nil {
log.Falal(err) // thrown, if database cannot be opened
}
// database connection is established, ready to perform migrations:
Auth = auth.New(&auth.Config{
DB: gormDB,
})
// Migrate AuthIdentity model, AuthIdentity will be used to save auth info, like username/password, oauth token, you could change that.
err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
if err != nil {
log.Fatal(err) // do not forget to throw exception, if migration fails
}
// Register Auth providers
// Allow use username/password
Auth.RegisterProvider(password.New(&password.Config{}))
err = gormDB.AutoMigrate(&auth_identity.AuthIdentity{})
if err != nil {
log.Fatal(err) // do not forget to throw exception, if migration fails
}
// Register Auth providers
// Allow use username/password
Auth.RegisterProvider(password.New(&password.Config{}))
mux := http.NewServeMux()
// Mount Auth to Router
mux.Handle("/auth/", Auth.NewServeMux())
http.ListenAndServe(":9000", manager.SessionManager.Middleware(mux))
}

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
問(wèn)題是沒(méi)有開(kāi)箱即用的支持。在教程中,他們忘記在導(dǎo)入中添加以下行:sqlite
_ "github.com/jinzhu/gorm/dialects/sqlite"
- 3 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報(bào)