Skip to content

Can't scan datetime into time.Time #316

@infogulch

Description

@infogulch

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions