MySQL插入Float32和Float64 Go [英] MySQL Insert Float32 and Float64 Go

查看:644
本文介绍了MySQL插入Float32和Float64 Go的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包主

我试图在MySQL表中插入一行import
strconv
io / ioutil
strings
os / exec
database / sql
_github .com / go-sql-driver / mysql


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

db,err:= sql.Open(mysql,user:pass @ / sysStats)
handleError(err)
$ (插入温度(id,cpu,gpu,timestamp)VALUES(?,?,?,?),1,temp_gpu,temp_cpu,time.Now())$ b(b $ b_,err = db.Query $ b handleError(err)

db.Close()
return
}

它的构建成功,但是当我运行产生的二进制文件时,它会在很长一段时间后出现超时错误。



以下模式:

  + ---------------- + ---- ------- ------ + + ----- + --------- +  - ------ + 
|字段|类型|空| Key |默认|额外|
+ ---------------- + ----------- + ------ + ----- + --- ------ + ------- +
| idtemperatures | int(11)| NO | PRI | NULL | |
| cpu | float |是| | NULL | |
| gpu | float |是| | NULL | |
|时间戳|时间戳|是| | NULL | |
+ ---------------- + ----------- + ------ + ----- + --- ------ + ------- +

我正在运行这个同样的服务器MySQL实例托管,我可以用终端用户/密码访问数据库。



任何帮助?



谢谢!

解决方案

你的db中的id / primary key字段叫做在您的SQL语句中使用了,并且您使用了id 你没有执行查询(但是 INSERT ),你应该使用 DB.Exec() 方法来执行它。您绝对不应该使用 DB.Query()

如果无法连接到数据库,则可能会出现超时错误。确保它使用默认协议(TCP)和主机(localhost:3306)。另一个原因可能是因为您使用 DB.Query()来执行您的SQL INSERT 语句,并且 DB.Query()返回一个 * sql.Rows 值,该值持有数据库连接,直到使用 Rows.Close() 方法 - 你永远不会做的;你甚至没有存储返回的 db.Rows 值。



你也应该调用 db.Close()作为延迟语句,在打开它之后进行错误检查。



更正后的示例:

  db,err:= sql.Open(mysql,user:pass @ / sysStats)
if err!= nil {
fmt.Println(无法打开DB:,错误)
返回
}
推迟db.Close()

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

if err!= nil {
fmt.Println(无法执行INSERT:,err)
} else {
n,err:= res.RowsAffected( )
fmt.Println(INSERT executed,affected affected:,n,err)
}


I'm trying to insert a row into a MySQL table:

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.

Any help?

Thanks!

解决方案

The id / primary key field in your db is called "idtemperatures" and you used "id" in your SQL statement.

Also since you're not executing a query (but an INSERT), you should use the DB.Exec() method to execute it. You should never use DB.Query() to execute DML (Data Manipulation Language) statements.

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). 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; you didn't even store the returned db.Rows value.

Also you should call db.Close() as a deferred statement, right after the error check after opening it.

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)
}

这篇关于MySQL插入Float32和Float64 Go的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆