如何修复golang sql字符串转换扫描错误

时间:2022-11-07 19:12:15

I'm receiving the following error when querying a row in an sqlite3 database:

我在查询sqlite3数据库中的一行时收到以下错误:

sql: Scan error on column index 3: converting string "" to a int64: strconv.ParseInt: parsing "": invalid syntax

在列索引3上扫描错误:将字符串“”转换为int64: strconv。解析:“”:无效语法

The query itself works, as it is able to return the queried data. I should note that each row in the database contains several columns with NULL data (only the Id and Email columns are populated). I am struggling to understand why the err occurs and how to resolve it.

查询本身可以工作,因为它能够返回查询的数据。我应该注意,数据库中的每一行都包含几个列,其中包含空数据(只填充Id和电子邮件列)。我正在努力理解为什么会出现这种错误,以及如何解决它。

The SQL table has the following nine columns:

SQL表有以下9列:

`0,Id,INTEGER,0,,1
1,Email,TEXT,0,,0
2,Name,TEXT,0,,0
3,Rsvp,INTEGER,0,,0
4,Guests,INTEGER,0,,0
5,Meal0,INTEGER,0,,0
6,Meal1,INTEGER,0,,0
7,Comments,TEXT,0,,0
8,ModifiedAt,TEXT,0,,0`

which are defined in server.go with the following struct (I added the sql.NullString to handle the empty columns):

在服务器中定义的。使用以下结构(我添加了sql)。空字符串用于处理空列):

type User struct {
    Id         int
    Email      string
    Name       sql.NullString
    Rsvp       sql.NullInt64
    Guests     sql.NullInt64
    Meal0      sql.NullInt64
    Meal1      sql.NullInt64
    Comments   sql.NullString
    ModifiedAt sql.NullString
}

the query is from the following handler:

查询来自以下处理程序:

func Rsvp1Handler(w http.ResponseWriter, r *http.Request) {
  email := r.URL.Path[len("/rsvp/"):]
  var user User
  err := userStatement.QueryRow(email).Scan(&user.Id, &user.Email, &user.Name, &user.Rsvp, &user.Guests, &user.Meal0, &user.Meal1, &user.Comments, &user.ModifiedAt)

  switch {
  case err == sql.ErrNoRows:
    log.Println("No user with that ID.")
    w.Header().Set("Content-Type", "text/plain")
    fmt.Fprintf(w, "Email address not found: %s", email)

  case err != nil:
    log.Println(err)
    fmt.Fprintf(w, "Database error:\n", err)
  default:
    log.Println("Query success. Email address: ", email)
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(&user)
}

which relies on the following constant and var:

它依赖于以下常数和var:

const userSelect  = "SELECT * FROM rsvp WHERE email = ?"
var userStatement   *sql.Stmt

Any help or insight into the error is greatly appreciated!

对于错误的任何帮助或见解,我们都非常感激!

1 个解决方案

#1


3  

It seems one of your columns (most likely Rsvp) is stored as a string type not int and it's set to "" instead of null.

看起来,您的列(很可能是Rsvp)是作为字符串类型而不是int存储的,它被设置为“”而不是null。

So you either have to change Rsvp (might be a different column) to sql.NullString, redo the database to have nulls instead of empty strings for numeric fields or if you believe it's a bug in the driver, open an issue on their tracker.

因此,您必须将Rsvp(可能是另一个列)更改为sql。NullString,重做数据库以使数字字段的空字符串变为空字符串,或者如果您认为驱动程序中有错误,则打开跟踪器上的问题。

#1


3  

It seems one of your columns (most likely Rsvp) is stored as a string type not int and it's set to "" instead of null.

看起来,您的列(很可能是Rsvp)是作为字符串类型而不是int存储的,它被设置为“”而不是null。

So you either have to change Rsvp (might be a different column) to sql.NullString, redo the database to have nulls instead of empty strings for numeric fields or if you believe it's a bug in the driver, open an issue on their tracker.

因此,您必须将Rsvp(可能是另一个列)更改为sql。NullString,重做数据库以使数字字段的空字符串变为空字符串,或者如果您认为驱动程序中有错误,则打开跟踪器上的问题。