Skip to content
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

Can't scan datetime into time.Time #316

Closed
infogulch opened this issue Jul 8, 2016 · 2 comments
Closed

Can't scan datetime into time.Time #316

infogulch opened this issue Jul 8, 2016 · 2 comments

Comments

@infogulch
Copy link

infogulch commented Jul 8, 2016

Trying to scan a *time.Time (select datetime('now')) using the below running example gives the error:

panic: cannot scan row: sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *time.Time

I would expect it to not give an error and instead populate the Time variable with the result, because time.Time/datetime is in the list of supported types.

However, changing the Time variable to string works as expected and outputs:

the time is: 2016-07-08 22:01:38

package main

import (
    "database/sql"
    "fmt"
    "time"

    _ "github.com/mattn/go-sqlite3"
)

func main() {
    db, err := sql.Open("sqlite3", ":memory:")
    if err != nil {
        panic(fmt.Sprintf("cannot open db: %v", err))
    }
    err = db.Ping()
    if err != nil {
        panic(fmt.Sprintf("cannot open db: %v", err))
    }
    // interesting stuff starts here
    row := db.QueryRow(`select datetime('now')`)
    var t time.Time
    // var t string  // NOTE: using this declaration for t works
    if err = row.Scan(&t); err != nil {
        panic(fmt.Sprintf("cannot scan row: %v", err))
    }
    fmt.Printf("the time is: %s\n", t)
}
@gwenn
Copy link

gwenn commented Jul 11, 2016

See issue #190

@infogulch
Copy link
Author

Thanks for the link. Closing as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants