Skip to content

Commit 7836ed0

Browse files
committed
update files 2019年 5月17日 星期五 18时17分33秒 CST
1 parent 5410e56 commit 7836ed0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

Diff for: errors.md

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ The mechanism to do this varies between drivers, however, because this isn't
153153
part of `database/sql` itself. In the MySQL driver that this tutorial focuses
154154
on, you could write the following code:
155155

156+
但是,执行此操作的机制因驱动程序而异,因为这不是 `database/sql` 本身的一部分。在本教程重点介绍的MySQL驱动程序中,您可以编写以下代码:
157+
156158
<pre class="prettyprint lang-go">
157159
if driverErr, ok := err.(*mysql.MySQLError); ok { // Now the error number is accessible directly
158160
if driverErr.Number == 1045 {
@@ -166,6 +168,8 @@ Again, the `MySQLError` type here is provided by this specific driver, and the
166168
is taken from MySQL's error message, and is therefore database specific, not
167169
driver specific.
168170

171+
同样,此处的MySQLError类型由此特定驱动程序提供,并且.Number字段可能因驱动程序而异。但是,数字的值取自MySQL的错误消息,因此是特定于数据库的,而不是特定于驱动程序的。
172+
169173
This code is still ugly. Comparing to 1045, a magic number, is a code smell.
170174
Some drivers (though not the MySQL one, for reasons that are off-topic here)
171175
provide a list of error identifiers. The Postgres `pq` driver does, for example, in
@@ -187,18 +191,24 @@ Handling Connection Errors
187191

188192
What if your connection to the database is dropped, killed, or has an error?
189193

194+
如果您的数据库连接被丢弃,被杀或有错误怎么办?
195+
190196
You don't need to implement any logic to retry failed statements when this
191197
happens. As part of the [connection pooling](connection-pool.html) in
192198
`database/sql`, handling failed connections is built-in. If you execute a query
193199
or other statement and the underlying connection has a failure, Go will reopen a
194200
new connection (or just get another from the connection pool) and retry, up to
195201
10 times.
196202

203+
发生这种情况时,您不需要实现任何逻辑来重试失败的语句。作为 `database/sql` 中连接池的一部分,内置处理失败的连接。如果您执行查询或其他语句并且底层连接出现故障,Go将重新打开一个新连接(或者从连接池中获取另一个连接)并重试最多10次。
204+
197205
There can be some unintended consequences, however. Some types of errors may be
198206
retried when other error conditions happen. This might also be driver-specific.
199207
One example that has occurred with the MySQL driver is that using `KILL` to
200208
cancel an undesired statement (such as a long-running query) results in the
201209
statement being retried up to 10 times.
202210

211+
但是,可能会产生一些意想不到的后果。当发生其他错误情况时,可能会重试某些类型的错误。这可能也是特定于驱动程序的。 MySQL驱动程序发生的一个例子是使用 `KILL` 取消不需要的语句(例如长时间运行的查询)会导致语句被重试最多10次。
212+
203213
**Previous: [Using Prepared Statements](prepared.html)**
204214
**Next: [Working with NULLs](nulls.html)**

Diff for: nulls.md

+19-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ Nullable columns are annoying and lead to a lot of ugly code. If you can, avoid
77
them. If not, then you'll need to use special types from the `database/sql`
88
package to handle them, or define your own.
99

10+
可空列很烦人,导致很多丑陋的代码。如果可以,请避免使用它们。如果没有,那么你需要使用 `database/sql` 包中的特殊类型来处理它们,或者定义你自己的类型。
11+
1012
There are types for nullable booleans, strings, integers, and floats. Here's how
1113
you use them:
1214

15+
有可空的布尔值,字符串,整数和浮点数的类型。这是你如何使用它们:
16+
1317
<pre class="prettyprint lang-go">
1418
for rows.Next() {
1519
var s sql.NullString
@@ -26,18 +30,28 @@ for rows.Next() {
2630
Limitations of the nullable types, and reasons to avoid nullable columns in case
2731
you need more convincing:
2832

33+
可空类型的限制,以及在需要更有说服力的情况下避免可空列的原因:
34+
2935
1. There's no `sql.NullUint64` or `sql.NullYourFavoriteType`. You'd need to
30-
define your own for this.
31-
1. Nullability can be tricky, and not future-proof. If you think something won't
36+
define your own for this.
37+
没有 `sql.NullUint64``sql.NullYourFavoriteType`。你需要为此定义自己的。
38+
2. Nullability can be tricky, and not future-proof. If you think something won't
3239
be null, but you're wrong, your program will crash, perhaps rarely enough
33-
that you won't catch errors before you ship them.
34-
1. One of the nice things about Go is having a useful default zero-value for
35-
every variable. This isn't the way nullable things work.
40+
that you won't catch errors before you ship them.
41+
可空性可能很棘手,而且不会出现面向未来的问题。如果您认为某些内容不会为空,但是您错了,那么您的程序将崩溃,或许很少,以至于在发送之前您不会发现错误。
42+
3. One of the nice things about Go is having a useful default zero-value for
43+
every variable. This isn't the way nullable things work.
44+
关于Go的一个好处是为每个变量提供了一个有用的默认零值。这不是可以为空的东西工作的方式。
3645

3746
If you need to define your own types to handle NULLs, you can copy the design of
3847
`sql.NullString` to achieve that.
3948

49+
如果需要定义自己的类型来处理NULL,可以复制sql.NullString的设计来实现它。
50+
4051
If you can't avoid having NULL values in your database, there is another work around that most database systems support, namely `COALESCE()`. Something like the following might be something that you can use, without introducing a myriad of `sql.Null*` types.
52+
53+
如果您无法避免在数据库中使用NULL值,那么大多数数据库系统都支持另一种解决方法,即 `COALESCE()`。像下面这样的东西可能是你可以使用的东西,而不会引入无数的 `sql.Null*` 类型。
54+
4155
<pre class="prettyprint lang-go">
4256
rows, err := db.Query(`
4357
SELECT

0 commit comments

Comments
 (0)