M
M
Miraid2022-01-09 22:08:00
MySQL
Miraid, 2022-01-09 22:08:00

Can't load golang values ​​into mysql database. how to solve it?

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

func main() {

    db, err := sql.Open("mysql", "mysql:[email protected](127.0.0.1:3306)/golang")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    insert, err := db.Query("INSERT INTO 'users' ('name', 'age') VALUES('alex', 43)")
    if err != nil {
        panic(err)
    }
    defer insert.Close()

    fmt.Println("ok")
}

here is the code, it gives an error,
61db31d58cf5f011523393.png
but if you remove the second part of the code
insert, err := db.Query("INSERT INTO 'users' ('name', 'age') VALUES('alex', 43)")
    if err != nil {
        panic(err)
    }
    defer insert.Close()

everything is fine
61db32333458a260388628.png
, tell me what the problem is and how to solve
it, the database itself
61db3273b7671631274799.png
is all local
61db32836ef1f026250506.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Mamonov, 2022-01-09
@Miraid

Your MySQL service is not running or MySQL is not listening to localhost:3306

G
galaxy, 2022-01-09
@galaxy

I don't know Go, but this is why (in the place where it stands): defer db.Close()?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question