插入查询在golang中不起作用

时间:2018-03-31 16:34:30

标签: mysql go

在我的golang程序中,插入MySQL查询没有插入值。甚至查询的语法都是正确的。它返回我的错误语句,即内部服务器错误

numElem

//连接到数据库 //您可能必须更改连接字符串以添加数据库凭据

            package main

            import (

            "database/sql"
            "net/http"
             "log"
            "golang.org/x/crypto/bcrypt"
            "encoding/json"
            _ "github.com/go-sql-driver/mysql"
            "fmt"

            )


            const hashCost = 8
            var db *sql.DB


            func initDB(){
                var err error

//创建一个结构,用于在请求正文和数据库

中为用户的结构建模
          db, err = sql.Open("mysql", 
     "root:nfn@tcp(127.0.0.1:3306)/mydb")

                if err != nil {
                    panic(err)
                }
            }

// Sql查询无法正常工作。如何解决此错误

                    type Credentials struct {
                    Password string `json:"password", db:"password"`
                      Username string `json:"username", db:"username"`
               }

            func Signup(w http.ResponseWriter, r *http.Request){
                // Parse and decode the request body into a new `Credentials` instance
                creds := &Credentials{}
                err := json.NewDecoder(r.Body).Decode(creds)
                if err != nil {
                    // If there is something wrong with the request body, return a 400 status
                    w.WriteHeader(http.StatusBadRequest)
                    return
                }
                // Salt and hash the password using the bcrypt algorithm
                // The second argument is the cost of hashing, which we arbitrarily set as 8 (this value can be more or less, depending on the computing power you wish to utilize)
                hashedPassword, err := bcrypt.GenerateFromPassword([]byte(creds.Password), 8)

                // Next, insert the username, along with the hashed password into the database

//如果我们正确存储在数据库中的凭据,并且发回了默认状态200,我们就达到了这一点                 }

0 个答案:

没有答案