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

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

How to update sqlite using go without other libraries

问题

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

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)

我尝试了很多方法,但没有找到一个可行的方法。像这个例子一样编译没有错误(通过恢复错误进行尝试),但在打开我的数据库后,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:

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.

答案1

得分: 1

这对我来说没问题:

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

英文:

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

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:

确定