如何在不使用其他库的情况下使用Go更新SQLite数据库?

huangapple go评论117阅读模式
英文:

How to update sqlite using go without other libraries

问题

嗨,我一整天都在为在Go中使用SQLite更新用户数据库中的secretQuestion和secretAnswer而苦恼,我在我的实际文件中有以下代码:

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

我尝试了很多方法,但没有找到一个可行的方法。像这个例子一样编译没有错误(通过恢复错误进行尝试),但在打开我的数据库后,secretQuestion和secretAnswer仍然为空。请注意,我已经检查过它们不是空值。

非常感谢您的帮助!我不太习惯使用论坛,如果需要添加其他信息,请随时告诉我。

英文:

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:

  1. r.ParseForm()
  2. id := r.URL.Query().Get("id")
  3. secretQuestion := r.Form.Get("question")
  4. secretAnswer, _ := bcrypt.GenerateFromPassword([]byte(r.Form.Get("answer")), 14)
  5. //
  6. database.Db, err = sql.Open("sqlite3", "./database/database.db")
  7. if err != nil {
  8. panic(err)
  9. }
  10. //
  11. result, _ := database.Db.Prepare("UPDATE users SET secretQuestion = ?,secretAnswer = ? WHERE id=?")
  12. 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.

答案1

得分: 1

这对我来说没问题:

  1. package main
  2. import (
  3. "database/sql"
  4. _ "github.com/mattn/go-sqlite3"
  5. )
  6. func main() {
  7. d, e := sql.Open("sqlite3", "file.db")
  8. if e != nil {
  9. panic(e)
  10. }
  11. defer d.Close()
  12. d.Exec("UPDATE artist_t SET check_s = ? WHERE artist_n = ?", "2021-05-20", 42)
  13. }

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

英文:

This works for me:

  1. package main
  2. import (
  3. "database/sql"
  4. _ "github.com/mattn/go-sqlite3"
  5. )
  6. func main() {
  7. d, e := sql.Open("sqlite3", "file.db")
  8. if e != nil {
  9. panic(e)
  10. }
  11. defer d.Close()
  12. d.Exec("UPDATE artist_t SET check_s = ? WHERE artist_n = ?", "2021-05-20", 42)
  13. }

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

huangapple
  • 本文由 发表于 2021年5月20日 01:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/67608290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定