@@ -64,6 +64,8 @@ that it's best to capture and check for errors in all database operations. If
64
64
Logging the error message or panicing might be the only sensible thing,
65
65
and if that's not sensible, then perhaps you should just ignore the error.
66
66
67
+ ` rows.Close() ` 返回的错误是一般规则的唯一例外,它最好捕获并检查所有数据库操作中的错误。如果rows.Close()返回错误,则不清楚应该怎么做。记录错误消息或panicing可能是唯一明智的事情,如果这不合理,那么也许你应该忽略错误。
68
+
67
69
Errors From QueryRow()
68
70
======================
69
71
@@ -81,15 +83,21 @@ fmt.Println(name)
81
83
What if there was no user with ` id = 1 ` ? Then there would be no row in the
82
84
result, and ` .Scan() ` would not scan a value into ` name ` . What happens then?
83
85
86
+ 如果没有id = 1的用户怎么办?然后结果中没有行,并且.Scan()不会将值扫描到名称中。那么会发生什么?
87
+
84
88
Go defines a special error constant, called ` sql.ErrNoRows ` , which is returned
85
89
from ` QueryRow() ` when the result is empty. This needs to be handled as a
86
90
special case in most circumstances. An empty result is often not considered an
87
91
error by application code, and if you don't check whether an error is this
88
92
special constant, you'll cause application-code errors you didn't expect.
89
93
94
+ Go定义了一个特殊的错误常量,称为sql.ErrNoRows,当结果为空时,它从QueryRow()返回。在大多数情况下,这需要作为特殊情况处理。应用程序代码通常不会将空结果视为错误,如果不检查错误是否为此特殊常量,则会导致您没有预料到的应用程序代码错误。
95
+
90
96
Errors from the query are deferred until ` Scan() ` is called, and then are
91
97
returned from that. The above code is better written like this instead:
92
98
99
+ 查询中的错误将被推迟,直到调用 ` Scan() ` ,然后从中返回。上面的代码更好地编写如下:
100
+
93
101
<pre class =" prettyprint lang-go " >
94
102
var name string
95
103
err = db.QueryRow("select name from users where id = ?", 1).Scan(& ; name)
@@ -110,6 +118,8 @@ to use this special-case in order to let the caller distinguish whether
110
118
you might not realize that your variable didn't get any value from the database
111
119
after all.
112
120
121
+ 有人可能会问为什么空结果集被认为是错误。空集没有任何错误。原因是QueryRow()方法需要使用这种特殊情况才能让调用者区分QueryRow()实际上是否找到了一行;没有它,Scan()不会做任何事情,你可能没有意识到你的变量毕竟没有从数据库中获得任何值。
122
+
113
123
You should only run into this error when you're using ` QueryRow() ` . If you
114
124
encounter this error elsewhere, you're doing something wrong.
115
125
0 commit comments