-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NULL float64 not working #26
Comments
Can you provide your table structure and your query string? |
Database struct
Method
Table
Data
|
Thanks! I'm working on it |
I have no clue why, but it seems to work for me with the current version. I tested both on Windows and a Linux VM. package main
import (
"database/sql"
"fmt"
_ "github.com/Go-SQL-Driver/MySQL"
//_ "./MySQL"
)
type PersonMetric struct {
Id int
PersonId int
ActualDateTime string //time.Time
Weight sql.NullFloat64
BodyFatPercentage sql.NullFloat64
SleepQuality sql.NullFloat64
SleepDuration sql.NullFloat64
OverallFeeling sql.NullFloat64
Comment sql.NullString
}
var db *sql.DB
func Setup() (err error) {
db, err = sql.Open("mysql", "top:secret@/?charset=utf8")
if err != nil {
return
}
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS `gotest`")
if err != nil {
return
}
_, err = db.Exec("USE gotest")
if err != nil {
return
}
_, err = db.Exec("DROP TABLE IF EXISTS `PersonMetric`")
if err != nil {
return
}
query := "CREATE TABLE `PersonMetric` (" +
"`Id` int(11) NOT NULL AUTO_INCREMENT," +
"`PersonId` int(11) DEFAULT NULL," +
"`ActualDateTime` datetime DEFAULT NULL," +
"`Weight` float DEFAULT NULL," +
"`BodyFatPercentage` float DEFAULT NULL," +
"`SleepQuality` float DEFAULT NULL," +
"`SleepDuration` float DEFAULT NULL," +
"`OverallFeeling` float DEFAULT NULL," +
"`Comment` text," +
"PRIMARY KEY (`Id`)" +
") ENGINE=InnoDB AUTO_INCREMENT=116 DEFAULT CHARSET=latin1;"
_, err = db.Exec(query)
if err != nil {
return
}
query = "INSERT INTO `PersonMetric` (`Id`, `PersonId`, `ActualDateTime`, `Weight`, `BodyFatPercentage`, `SleepQuality`, `SleepDuration`, `OverallFeeling`, `Comment`) VALUES" +
"(1, 2, '2011-10-22 00:00:00', 69.3, 10.3, 6, 9, 7, NULL)," +
"(2, 2, '2011-10-23 00:00:00', 70.1, 9.6, 8, 9, 8, NULL)," +
"(3, 2, '2011-10-24 00:00:00', 69.9, 9.5, 6, 8, 9, NULL)," +
"(4, 2, '2011-11-05 00:00:00', 70.4, 9.6, 8, 8, 7, NULL)," +
"(5, 2, '2011-11-17 00:00:00', 69.6, 9.1, 8, 7.5, 8, NULL)," +
"(6, 2, '2011-11-25 00:00:00', 70.6, 9.6, 8, 7, 5, NULL)," +
"(7, 2, '2011-12-21 00:00:00', 70.4, 9.4, 6, 6, 10, NULL)," +
"(8, 2, '2012-01-03 00:00:00', 71.6, 10.1, 6, 8, 7, NULL)," +
"(9, 2, '2012-01-11 00:00:00', 70.1, 9.6, 7, 8, 5, NULL)," +
"(10, 2, '2012-01-12 00:00:00', 69.7, 9.4, 5, 7, 5, NULL)," +
"(11, 2, '2012-01-26 00:00:00', 69.9, 9.8, 8, 8, 6, NULL)," +
"(12, 2, '2011-10-25 00:00:00', 70.5, 11.5, 6, 7.5, 6, NULL)," +
"(13, 2, '2011-10-26 00:00:00', 70.1, 9.5, 7, 8, 7, NULL)," +
"(14, 2, '2011-10-27 00:00:00', 69.5, 9.5, 8, 8, 7, NULL)," +
"(15, 2, '2011-10-28 00:00:00', 69.3, 9.5, 8, 9, 8, NULL)," +
"(16, 2, '2011-10-29 00:00:00', 70.5, 11.5, 7, 9, 6, NULL)," +
"(17, 2, '2011-10-30 00:00:00', 69.8, 9.9, 7, 9, 7, NULL)," +
"(18, 2, '2011-07-20 00:00:00', 74.3, 11, 7, 8, 7, NULL)," +
"(19, 2, '2011-11-01 00:00:00', 69.5, 9.5, 8, 9, 7, NULL)," +
"(20, 2, '2011-09-30 00:00:00', 69.3, NULL, 5, 9, 7, NULL)," +
"(21, 2, '2011-09-29 00:00:00', 69.7, NULL, 5, 8, 7, NULL)," +
"(22, 2, '2011-09-15 00:00:00', 69.8, NULL, 7, 7.5, 7, NULL)," +
"(23, 2, '2011-11-02 00:00:00', 70.5, 9.5, 8, 8, 6, NULL)," +
"(24, 2, '2011-11-03 00:00:00', 69.6, 9.6, 6, 7, 6, NULL)," +
"(25, 2, '2011-11-04 00:00:00', 70.6, 9.8, 6, 7, 5, NULL)," +
"(26, 2, '2011-11-06 00:00:00', 69.5, 9.2, 7, 9, 7, NULL)," +
"(27, 2, '2011-11-07 00:00:00', 69.7, 9.7, 7, 8, 7, NULL)," +
"(28, 2, '2011-11-08 00:00:00', 69.2, 8.5, 8, 7.5, 8, NULL)," +
"(29, 2, '2011-11-09 00:00:00', 69, 10.3, 8, 8, 7, NULL)," +
"(30, 2, '2011-11-12 00:00:00', 70.6, 9.6, 8, 8, 7, NULL)," +
"(31, 2, '2011-11-16 00:00:00', 70.6, 9.6, 8, 8, 6, NULL)," +
"(32, 2, '2011-11-18 00:00:00', 69.8, 9.2, NULL, 8, 7, NULL)," +
"(33, 2, '2011-11-10 00:00:00', 69.3, 10.3, 8, 7, 6, NULL)," +
"(34, 2, '2011-11-11 00:00:00', 70.3, 11.3, 8, 7, 7, NULL)," +
"(35, 2, '2011-11-13 00:00:00', 70.2, 9.6, 7, 8, 7, NULL)," +
"(36, 2, '2011-11-15 00:00:00', 70.9, 9.7, 8, 8, 7, NULL)," +
"(37, 2, '2011-11-14 00:00:00', 70.9, 9.7, 8, 8, 7, NULL)," +
"(38, 2, '2011-11-19 00:00:00', 70.6, 9.6, 7, 7.5, 6, NULL)," +
"(39, 2, '2011-11-21 00:00:00', 71.5, 10.1, 6, 7, 8, NULL)," +
"(40, 2, '2011-11-22 00:00:00', 70.5, 9.6, 4, 6, 5, NULL)," +
"(41, 2, '2011-11-23 00:00:00', 70, 11.2, 7, 7, 8, NULL)," +
"(42, 2, '2011-11-27 00:00:00', 70.8, 9.6, 7, 6, 6, NULL)," +
"(43, 2, '2011-11-28 00:00:00', 70.5, 9.2, 6, 8, 6, NULL)," +
"(44, 2, '2011-11-29 00:00:00', 70.5, 11.3, 8, 7, 7, NULL)," +
"(45, 2, '2011-11-30 00:00:00', 70.8, 9.8, 8, 8, 8, NULL)," +
"(46, 2, '2011-12-01 00:00:00', 70.6, 10.3, 8, 8, 7, NULL)," +
"(47, 2, '2011-12-02 00:00:00', 70.6, 10.3, 8, 8, 7, NULL)," +
"(48, 2, '2011-12-05 00:00:00', 71.6, 10.4, 8, 7, 6, NULL)," +
"(49, 2, '2011-12-06 00:00:00', 70.4, 9.5, 8, 8, 6, NULL)," +
"(50, 2, '2011-12-07 00:00:00', 70.2, 9.7, 6, 9, 8, NULL)," +
"(51, 2, '2011-12-09 00:00:00', 71.4, 10.2, 8, 8, 8, NULL)," +
"(52, 2, '2011-12-12 00:00:00', 71.1, 10.1, 8, NULL, 7, NULL)," +
"(53, 2, '2011-12-13 00:00:00', 69.9, 9.5, 7, 8, 7, NULL)," +
"(54, 2, '2011-12-14 00:00:00', 69.9, 9.5, 8, 9, 8, NULL)," +
"(55, 2, '2011-12-16 00:00:00', 69.4, 9.4, 7, 8, 8, NULL)," +
"(56, 2, '2011-12-13 00:00:00', 69.4, 9.4, 7, 9, 8, NULL)," +
"(57, 2, '2011-12-20 00:00:00', 70.4, 9.3, 8, 7, 10, NULL)," +
"(58, 2, '2012-02-08 00:00:00', 70.6, 9.9, 7, 7, 8, NULL)," +
"(59, 2, '2012-01-04 00:00:00', 71.8, 12.1, 8, 8, 8, NULL)," +
"(60, 2, '2012-01-05 00:00:00', 71.1, 11.1, 6, 8, 8, NULL)," +
"(61, 2, '2012-01-06 00:00:00', 70.4, 10, 8, 8, 8, NULL)," +
"(62, 2, '2012-01-08 00:00:00', 70.6, 10.2, 8, 10, 6, NULL)," +
"(63, 2, '2012-01-10 00:00:00', 70.3, 9.5, 5, 8, 6, NULL)," +
"(64, 2, '2012-01-13 00:00:00', 69.2, 9.1, 8, 7, 7, NULL)," +
"(65, 2, '2012-01-17 00:00:00', 69.6, 9.1, 8, 8, 7, NULL)," +
"(66, 2, '2012-05-20 00:00:00', 72.5, 20, 8, 8, NULL, NULL)," +
"(67, 2, '2012-05-21 00:00:00', 71.2, 19.2, 8, 8, 8, NULL)," +
"(68, 2, '2012-03-20 00:00:00', 69.3, 8.8, 8, 8, 8, NULL)," +
"(69, 2, '2012-03-23 00:00:00', 70.3, 9.5, 8, 8, 8, NULL)," +
"(70, 2, '2012-01-18 00:00:00', 70.5, 9.6, 8, 9, 8, NULL)," +
"(71, 2, '2012-01-23 00:00:00', 71.4, 9.6, 7, 8, 9, NULL)," +
"(72, 2, '2012-01-24 00:00:00', 70.4, 9.2, 8, 8, 8, NULL)," +
"(73, 2, '2012-01-25 00:00:00', 70.3, 10, 8, 8, 10, NULL)," +
"(74, 2, '2012-01-27 00:00:00', 70.2, 10, 8, 9, 7, NULL);"
res, err := db.Exec(query)
if err != nil {
return
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return
}
lastInsertId, err := res.LastInsertId()
if err != nil {
return
}
fmt.Println("RowsAffected: ", rowsAffected, " LastInserID: ", lastInsertId)
return
}
func GetByPersonId(personId int) (*[]PersonMetric, error) {
rows, err := db.Query("SELECT Id, PersonId, ActualDateTime, Weight, BodyFatPercentage, SleepQuality, SleepDuration, OverallFeeling, Comment FROM PersonMetric WHERE PersonId=?;", personId)
var list []PersonMetric
if err == nil {
for rows.Next() {
var personMetric PersonMetric
err := rows.Scan(&personMetric.Id, &personMetric.PersonId, &personMetric.ActualDateTime, &personMetric.Weight, &personMetric.BodyFatPercentage, &personMetric.SleepQuality, &personMetric.SleepDuration, &personMetric.OverallFeeling, &personMetric.Comment)
if err != nil {
return nil, err
}
list = append(list, personMetric)
}
}
return &list, err
}
func main() {
err := Setup()
if err != nil {
panic(err)
}
list, err := GetByPersonId(2)
if err != nil {
panic(err)
}
for i, personMetric := range *list {
fmt.Print(i, `: `, personMetric.Id, ` | `, personMetric.ActualDateTime, ` | `)
if personMetric.Weight.Valid {
fmt.Print(personMetric.Weight.Float64)
} else {
fmt.Print(nil)
}
fmt.Print(` | `)
if personMetric.BodyFatPercentage.Valid {
fmt.Print(personMetric.BodyFatPercentage.Float64)
} else {
fmt.Print(nil)
}
fmt.Print(` | `)
if personMetric.SleepQuality.Valid {
fmt.Print(personMetric.SleepQuality.Float64)
} else {
fmt.Print(nil)
}
fmt.Print(` | `)
if personMetric.SleepDuration.Valid {
fmt.Print(personMetric.SleepDuration.Float64)
} else {
fmt.Print(nil)
}
fmt.Print(` | `)
if personMetric.OverallFeeling.Valid {
fmt.Print(personMetric.OverallFeeling.Float64)
} else {
fmt.Print(nil)
}
fmt.Print(` | `)
if personMetric.Comment.Valid {
fmt.Print(personMetric.Comment.String)
} else {
fmt.Print(nil)
}
fmt.Println()
}
} Output:
Can you confirm, that it works now? Or did I miss something? 😮 Don't forget to update with |
I have downloaded the latest version of the driver with
go get github.com/Go-SQL-Driver/MySQL
I am not getting errors is I use sql.NullString when loading null values but I am still getting the error for sql.NullFloat64
sql: Scan error on column index 4: converting string "" to a float64: strconv.ParseFloat: parsing "": invalid syntax
Is there only support for null strings? or should this be working for null float64?
The text was updated successfully, but these errors were encountered: