I am writing this program with a user database to manage my projects, but right now I have this issue with the database/sql library, which gives me this error.
The code I am calling that function from is this:
func CreateNewUser(username string, password string, dbDir string, saltOutputDir string) (bool, error) {
// Check if the database file exist
_, err := DBinit(dbDir)
if err != nil {
return false, err
}
mainDb, err := sql.Open("sqlite", dbDir+"/users.db") // "./db_manage/database/users.db"
if err != nil {
// warn with log
return false, err
}
// Check if the user already exists
var checkUser string
err = mainDb.QueryRow("SELECT Username FROM users WHERE Username = ?", username).Scan(&checkUser)
if err != sql.ErrNoRows {
return false, errors.New("user already exists")
}
// Generate the token
token, err := GenerateToken(username, password, saltOutputDir)
if err != nil {
return false, err
}
// Insert the user into the database
_, err = mainDb.Exec("INSERT INTO users (Username, TOKEN) VALUES (?, ?)", username, token)
if err != nil {
return false, err
}
defer func(mainDb *sql.DB) {
err := mainDb.Close()
if err != nil {
log.Fatal(ErrorMsg(err.Error()))
}
}(mainDb)
return true, nil
}
Btw the DBinit function just checks if the database file exists and creates it if it does not.
All required photos are given.