提交哈希到 SQLite 数据库时出错,使用的是 Golang 语言。

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

Error submitting a hash to sqlite db golang

问题

尝试将表单中的哈希值发送到SQLite数据库

func Signup(r http.ResponseWriter, w *http.Request)  {
	w.ParseForm()
	email := w.Form.Get("Email")
	if !govalidator.IsEmail(email) {
		utils.ServerResponse("email is incorrect", "frontend/index.html", r)
		return
	}

	password := w.Form.Get("Password")
	hash, err := bcrypt.GenerateFromPassword([]byte("Password"), bcrypt.DefaultCost)
	if password == "" || len(password) < 6 {
		utils.ServerResponse("Password length must be longer than 6 letters and must contain numbers and uppercase letters", "frontend/index.html", r)
		return
	}

	query := fmt.Sprintf("INSERT INTO User (email, password) VALUES ('%s', '%s')", email, string(hash))

    fmt.Println(query, string(hash)) // 用于调试的打印语句

	statement , err := db.SqliteDatabase.Prepare(query)

终端输出并且没有提交到数据库:

INSERT INTO User (email, password)
                VALUES ("email" , "hash")
        %!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
near "%": 语法错误
英文:

Trying to send a hash from a form to an sqlite database

func Signup(r http.ResponseWriter, w *http.Request)  {
	w.ParseForm()
	email := w.Form.Get(&quot;Email&quot;)
	if !govalidator.IsEmail(email) {
		utils.ServerResponse(&quot;email is incorrect&quot;, &quot;frontend/index.html&quot;, r)
		return
	}

	password := w.Form.Get(&quot;Password&quot;)
	hash, err := bcrypt.GenerateFromPassword([]byte(&quot;Password&quot;), bcrypt.DefaultCost)
	if password == &quot;&quot; || len(password) &lt; 6 {
		utils.ServerResponse(&quot;Password length must be longer than 6 letters and must contain numbers and uppercase letters&quot;, &quot;frontend/index.html&quot;, r)
		return
	}

	query := fmt.Sprintf(&quot;INSERT INTO User (email, password)&quot;, VALUES &quot;email&quot;, &quot;hash&quot;)

    fmt.Println(query, string(hash)) // put this here for debugging

	statement , err := db.SqliteDatabase.Prepare(query)

Output in the terminal and nothing submits to the database:

INSERT INTO User (email, password)
                VALUES (&quot;email&quot; , &quot;hash&quot;)
        %!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
near &quot;%&quot;: syntax error

答案1

得分: 1

发现我在使用fmt.Sprintf函数时出现了错误,并进行了修复。

query := fmt.Sprintf(`
		INSERT INTO User (email, password)
		VALUES ("%s" , "%s")
	`, email, hash)
英文:

Discovered i was using the fmt.Sprinf function incorrectly and fixed it.

query := fmt.Sprintf(`
		INSERT INTO User (email, password)
		VALUES (&quot;%s&quot; , &quot;%s&quot;)
	`, email, hash)

huangapple
  • 本文由 发表于 2021年8月12日 07:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/68750107.html
匿名

发表评论

匿名网友

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

确定