在使用Ubuntu 12.04和Oracle时出现了错误字符。

huangapple go评论85阅读模式
英文:

Wrong characters On GO witn UBUNTU 12.04 and Oracle

问题

我已经设置了一个带有Ubuntu 12.04 LTS(64位)、Go 1.2.2和go-oci8(github.com/mattn/go-oci8)的机器,但是当我在Oracle 11G数据库上执行查询时,返回的俄语和中文字符是错误的。

我尝试在我的项目中设置NLS_LANG字段,如下所示:

os.Setenv("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.AL32UTF8")
os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
os.Setenv("NLS_LANG", "RUSSIAN_CIS.AL32UTF8")

有人可以帮助我吗?我应该设置什么样的NLS_LANG配置才能获得正确的俄语、中文或其他与英语和西班牙语不同的语言的字符?

这是一个示例:

创建表room
{
  name_spainish varchar2(100),
  name_chinesses varchar2(100),
  name_russian varchar2(100)
}
insert into room (name_spainish, name_chinesses, name_russian) values ('Habitación doble','雙人房','двухместный номер')
package main

import (
  "database/sql"
  "fmt"
  "os"
  _ "github.com/mattn/go-oci8"
)

func main() {
   ExecQuery("select name_spainish, name_chinesses, name_russian from room")
 }

func ExecQuery(query string){
os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")

db, err := sql.Open("oci8", "user/userps@host:1521/SID")
if err != nil {
   fmt.Println(err)
   return
}
rows, err := db.Query(query)
if err != nil {
   fmt.Println(err)
   return
}

for rows.Next() {
	var nameSpainish string
	var nameChinesses string
	var nameRussian string
	rows.Scan(&nameSpainish, &nameChinesses, &nameRussian)
	fmt.Printf("Name Spainish %s - Name Chinesses %s - Name Russian %s", nameSpainish, nameChinesses, nameRussian)
}
rows.Close()
db.Close()
}

当前输出:
Habitación doble ???? ?????????

期望输出:
Habitación doble 雙人房 двухместный номер

英文:

I've got set up a machine with Ubuntu 12.04 LTS(64bits), Go 1.2.2 and go-oci8(github.com/mattn/go-oci8), but when I executed a query on Database Oracle 11G, it returned wrong characters on Russian and chinese.

I've tried to set up the NLS_LANG field on my proyect as is show it below:

os.Setenv("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.AL32UTF8")
os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")
os.Setenv("NLS_LANG", "RUSSIAN_CIS.AL32UTF8")

Someone can help me, what configuration of NLS_LANG I should put to get the right characters on Russian, Chinese or other difference languages than English and Spanish?

This is the example:

1.

Create table room 
{
  name_spainish varchar2(100),
  name_chinesses varchar2(100),
  name_russian varchar2(100)
}

2.

insert into room (name_spainish, name_chinesses, name_russian) values ('Habitación doble','雙人房','двухместный номер')

3.

package main

import (
  "database/sql"
  "fmt"
  "os"
  _ "github.com/mattn/go-oci8"
)

func main() {
   ExecQuery("select name_spainish, name_chinesses, name_russian from room")
 }

func ExecQuery(query string){
os.Setenv("NLS_LANG", "AMERICAN_AMERICA.AL32UTF8")

db, err := sql.Open("oci8", "user/userps@host:1521/SID")
if err != nil {
   fmt.Println(err)
   return
}
rows, err := db.Query(query)
if err != nil {
   fmt.Println(err)
   return
}

for rows.Next() {
	var nameSpainish string
	var nameChinesses string
	var nameRussian string
	rows.Scan(&nameSpainish, &nameChinesses, &nameRussian)
	fmt.Printf("Name Spainish %s - Name Chinesses %s - Name Russian %s", nameSpainish, nameChinesses, nameRussian)
}
rows.Close()
db.Close()
}

current output:
Habitación doble ???? ?????????

expect output:
Habitación doble 雙人房 двухместный номер

答案1

得分: 0

这个问题已经通过将值转换为十六进制来解决,使用以下查询:

select RAWTOHEX(UTL_RAW.cast_to_raw(name_spainish, name_chinesses, name_russian)) from room

在代码中,添加了encoding/hex包和hex.DecodeString(texto)函数。

for rows.Next() {
    var nameSpainish string
    var nameChinesses string
    var nameRussian string
    rows.Scan(&nameSpainish, &nameChinesses, &nameRussian)
    dataSpainish, _ := hex.DecodeString(nameSpainish)
    dataChinesses, _ := hex.DecodeString(nameChinesses)
    dataRussian, _ := hex.DecodeString(nameRussian)
    fmt.Printf("Name Spainish %s - Name Chinesses %s - Name Russian %s", string(dataSpainish), string(dataChinesses), string(dataRussian))
}
英文:

This problem had been solved converting the values into hexadecimal using the follow query

select RAWTOHEX(UTL_RAW.cast_to_raw(name_spainish, name_chinesses, name_russian)) from room 

On code was adding the package encoding/hex and the function hex.DecodeString(texto).

for rows.Next() {
	var nameSpainish string
	var nameChinesses string
	var nameRussian string
	rows.Scan(&nameSpainish, &nameChinesses, &nameRussian)
	dataSpainish, _ := hex.DecodeString(nameSpainish)
	dataChinesses, _ := hex.DecodeString(nameChinesses)
	dataRussian, _ := hex.DecodeString(nameRussian)
	fmt.Printf("Name Spainish %s - Name Chinesses %s - Name Russian %s", string(dataSpainish), string(dataChinesses), string(dataRussian))
}

huangapple
  • 本文由 发表于 2014年6月12日 23:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/24187865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定