Virgil Nauleau
Virgil Nauleau

Reputation: 139

How to update sqlite using go without other libraries

Hi ive been in trouble all the day finding a way to update secretQuestion and secretAnswer in my user database in sqlite using go, what i have in my actual file is:

r.ParseForm()
        id := r.URL.Query().Get("id")
        secretQuestion := r.Form.Get("question")
        secretAnswer, _ := bcrypt.GenerateFromPassword([]byte(r.Form.Get("answer")), 14)
        //
        database.Db, err = sql.Open("sqlite3", "./database/database.db")
        if err != nil {
            panic(err)
        }
        //
        result, _ := database.Db.Prepare("UPDATE users SET secretQuestion = ?,secretAnswer = ? WHERE id=?")
        result.Exec(secretQuestion, secretAnswer, id)

I didnt found a single way that work and ive tried a good amount, those like this one compile and dont give error (tryed by recovering the err) but after opening my database secretQuestion and secretAnswer are still nill, note that what I gave them is not nill already checked that. Thanks per advance for the help ! I'm not used to used forum so feel free to tell me if I need to add something.

Upvotes: 0

Views: 2675

Answers (1)

Zombo
Zombo

Reputation: 1

This works for me:

package main

import (
   "database/sql"
   _ "github.com/mattn/go-sqlite3"
)

func main() {
   d, e := sql.Open("sqlite3", "file.db")
   if e != nil {
      panic(e)
   }
   defer d.Close()
   d.Exec("UPDATE artist_t SET check_s = ? WHERE artist_n = ?", "2021-05-20", 42)
}

https://github.com/mattn/go-sqlite3

Upvotes: 1

Related Questions