为了表达我的问题很复杂,我尝试:
我有一个DB2表(内部代码页CP850)
create table codes (code character(2))
使用utf-8字符数据集
insert into codes values('ÖÖ')
在linux下,db2客户端select * from codes
提供
code
ÖÖ
正如预期的那样。
现在对于围棋部分,这是有问题的
我的型号看起来像
package codes
type Code struct {
Code db.NullString `json:"code" sql:"code"`
}
type Codes []*Code
我的查询看起来像
package codes
func FindAll()(Codes, error) {
result := make(Codes, 0)
row, err := db.Query(nil, 'SELECT c.code FROM codes c');
//errorhandling...
for rows.Next() {
c := &Code{}
rows.Scan(&c.Code)
log.PrintLn(c.Code) // <== 'Ö'
result = append(result, c)
}
return result, nil
}
此日志只打印一个Ö
,而不是同时打印两个。我知道这是因为符文Ö
在内存中不是一个字节。但我不知道该去哪里寻找和修复,因为这是我第一次尝试Go。
(缺少什么?我可以提供)
你不能扫描到你的结构中,你必须扫描到sql.NullString
变量或string
,然后分配给你的结构:
for rows.Next() {
var s sql.NullString
rows.Scan(&s)
c := Code{Code: s}
log.PrintLn(c.Code) // <== 'Ö'
result = append(result, c)
}
此外:字段标记sql
没有效果(或者你使用sqlx?如果是,你必须使用StructScan而不是Scan)