英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论