简体   繁体   English

MySQL插入Float32和Float64 Go

[英]MySQL Insert Float32 and Float64 Go

I'm trying to insert a row into a MySQL table: 我正在尝试在MySQL表中插入一行:

package main

import (
    "strconv"
    "io/ioutil"
    "strings"
    "os/exec"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    temp_cpu := getCPUTemp() // returns float32
    temp_gpu := getGPUTemp() // returns float64

    db, err := sql.Open("mysql", "user:pass@/sysStats")
    handleError(err)

    _, err = db.Query("INSERT INTO temperatures (id, cpu, gpu, timestamp) VALUES (?, ?, ?, ?)", 1, temp_gpu, temp_cpu, time.Now())
    handleError(err)

    db.Close()
    return
}

It builds successfully but when I run the resulting binary it just times out after a long time with a generic timeout error. 它可以成功构建,但是当我运行生成的二进制文件时,经过很长一段时间后它才超时,并出现通用超时错误。

The table has the following schema: 该表具有以下架构:

+----------------+-----------+------+-----+---------+-------+
| Field          | Type      | Null | Key | Default | Extra |
+----------------+-----------+------+-----+---------+-------+
| idtemperatures | int(11)   | NO   | PRI | NULL    |       |
| cpu            | float     | YES  |     | NULL    |       |
| gpu            | float     | YES  |     | NULL    |       |
| timestamp      | timestamp | YES  |     | NULL    |       |
+----------------+-----------+------+-----+---------+-------+

I am running this on the same server the MySQL instance is hosted and I can access the database with the user/password from the terminal. 我在托管MySQL实例的同一台服务器上运行此服务器,并且可以从终端使用用户名/密码访问数据库。

Any help? 有什么帮助吗?

Thanks! 谢谢!

The id / primary key field in your db is called "idtemperatures" and you used "id" in your SQL statement. 数据库中的id /主键字段称为"idtemperatures"并且您在SQL语句中使用了"id"

Also since you're not executing a query (but an INSERT ), you should use the DB.Exec() method to execute it. 另外,由于您不执行查询(而是INSERT ),因此应该使用DB.Exec()方法执行查询。 You should never use DB.Query() to execute DML (Data Manipulation Language) statements. 您永远不要使用DB.Query()执行DML(数据操作语言)语句。

You may get a timeout error if you can't connect to your database. 如果您无法连接到数据库,则可能会收到超时错误。 Make sure it uses default protocol (TCP) and host (localhost:3306). 确保它使用默认协议(TCP)和主机(localhost:3306)。 Another reason may be because you used DB.Query() to execute your SQL INSERT statement, and DB.Query() returns an *sql.Rows value which holds a database connection until you close it with the Rows.Close() method - which you never do; 另一个原因可能是因为您使用DB.Query()执行SQL INSERT语句,并且DB.Query()返回一个*sql.Rows值,该值保持数据库连接,直到使用Rows.Close()方法关闭它为止-你永远不会做 you didn't even store the returned db.Rows value. 您甚至都没有存储返回的db.Rows值。

Also you should call db.Close() as a deferred statement, right after the error check after opening it. 另外,您应该在打开错误检查后立即将db.Close()作为延迟语句调用。

Corrected example: 更正的示例:

db, err := sql.Open("mysql", "user:pass@/sysStats")
if err != nil {
    fmt.Println("Failed to open DB:", err)
    return
}
defer db.Close()

s := "INSERT INTO temperatures (idtemperatures, cpu, gpu, timestamp) VALUES (?, ?, ?, ?)"
res, err = db.Exec(s, 1, temp_gpu, temp_cpu, time.Now())

if err != nil {
    fmt.Println("Failed to execute INSERT:", err)
} else {
    n, err := res.RowsAffected()
    fmt.Println("INSERT executed, rows affected: ", n, err)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM