GBase 8s
其他
问答
Golang 查询带有 text 字段的数据报错
发表于2024-07-03 10:00:41103次浏览8个评论
为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【GBase版本】: GBase8sV8.8_TL_3.5.1_x86_64
【操作系统】: Linux version 5.10.0-60.18.0.50.oe2203.x86_64
【CPU】: CPU E3-1245 v6
【问题描述】*: Golang 查询带有 text 字段的数据报错
表结构如下
CREATE TABLE template
(
id serial(499) NOT NULL PRIMARY KEY,
name varchar(64) NOT NULL,
content text
);
执行SQL: select id, name, content from template order by id DESC LIMIT 10;
错误信息:&odbc.Error{APIName:"SQLGetData", Diag:[]odbc.DiagRecord{odbc.DiagRecord{State:"HY000", NativeError:-11003, Message:"[GBasedbt][GBasedbt ODBC Driver]Unspecified System Error = -11003."}}}
评论
登录后才可以发表评论
liaosnet发表于 2年前
小龙虾发表于 2年前
@liaosnet:您好,感谢您的回复
我按照你的代码,单独测试了自己的功能;
当我把我的text字段(对应你代码里的col3)改成简单的字符串,或者小的json字符串是没有问题的;
当我换成较大的json就会出现错误,我不确定是某个json key的问题,我的实例代码如下
我按照你的代码,单独测试了自己的功能;
当我把我的text字段(对应你代码里的col3)改成简单的字符串,或者小的json字符串是没有问题的;
当我换成较大的json就会出现错误,我不确定是某个json key的问题,我的实例代码如下
小龙虾发表于 2年前
@小龙虾:PS:数据插入是成功的,我可以在终端看到插入的数据
小龙虾发表于 2年前
@liaosnet:package main
import (
"database/sql"
"encoding/json"
"fmt"
"time"
_ "github.com/alexbrainman/odbc"
)
/*
DROP TABLE IF EXISTS template;
CREATE TABLE template
(
id serial(499) NOT NULL PRIMARY KEY,
name varchar(64) NOT NULL,
descript VARCHAR(255) NOT NULL DEFAULT '',
create_time timestamp NOT NULL,
update_time timestamp NOT NULL,
content text DEFAULT NULL
);
*/
const (
DSN = "table_name"
PWD = "password"
ODBC = "odbc"
)
func NowTime() string {
return time.Now().Format(time.DateTime)
}
func AssertError(msg string, err error) {
if err != nil {
panic(fmt.Errorf("msg:%s, err:%s", msg, err))
}
}
type EnTemplate struct {
Id int `json:"id"`
Name string `json:"name"`
Desc string `json:"descript"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
Content *Content `json:"content"`
}
type Content struct {
V1 string `json:"v11111111111111111"`
V2 bool `json:"v22222222222222222"`
V3 int `json:"v33333333333333333"`
V4 int `json:"v4_measurement_measurement_measurement"`
V5 string `json:"v555555555555555555"`
V6 int `json:"v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V7 int `json:"v7v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V8 string `json:"v8v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V9 string `json:"v9v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V10 string `json:"v10v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V11 interface{} `json:"v11v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V12 string `json:"v12v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V13 string `json:"v13v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V14 bool `json:"v14v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V15 string `json:"v15v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V16 string `json:"v16v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V17 string `json:"v17v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V18 string `json:"v18v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V19 int `json:"v19v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V20 string `json:"v20v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V21 string `json:"v21v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V22 string `json:"v22v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V23 string `json:"v23v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V24 string `json:"v24v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V25 string `json:"v25v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V26 string `json:"v26v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V27 string `json:"v27v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V28 string `json:"v28v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V29 int64 `json:"v29v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V30 string `json:"v30v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V31 string `json:"v31v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V32 string `json:"v32v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V33 string `json:"v33v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V34 bool `json:"v34v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
}
var prepareData = &Content{
V1: "Hello_world",
V2: false,
V3: 1000,
V4: 2000,
V5: "Hello_world",
V6: 20000,
V7: 80000,
V8: "Hello_world",
V9: "Hello_world",
V10: "Hello_world",
V11: nil,
V12: "Hello_world",
V13: "Hello_world",
V14: false,
V15: "none",
V16: "1",
V17: "Hello_world",
V18: "Hello_world",
V19: -1,
V20: "Hello_world",
V21: "-1",
V22: "-1",
V23: "default",
V24: "-1",
V25: "0",
V26: "Auto",
V27: "Hello_world",
V28: "Hello_world",
V29: 0,
V30: "34",
V31: "2",
V32: "64",
V33: "24",
V34: false,
}
func main() {
dbSourceName := fmt.Sprintf("DSN=%s;UID=gbasedbt;PWD=%s;CHARSET=zh_CN.utf8;", DSN, PWD)
conn, err := sql.Open(ODBC, dbSourceName)
AssertError("open", err)
b, err := json.Marshal(prepareData)
AssertError("marshal", err)
query := "INSERT INTO template(name, descript, create_time, update_time, content) values(?, ?, ?, ?, ?)"
args := []interface{}{
"test", "", NowTime(), NowTime(), string(b),
}
rest, err := conn.Exec(query, args...)
AssertError("exec", err)
fmt.Printf("insert success; rest:%#v\n", rest)
// my query
var tpls []*EnTemplate
query = "SELECT id, name, descript, create_time, update_time, content FROM template ORDER BY id DESC LIMIT 10 OFFSET 0"
rows, err := conn.Query(query)
AssertError("query", err)
defer rows.Close()
for rows.Next() {
t := &EnTemplate{}
var contentByte []byte
var content = &Content{}
err = rows.Scan(&t.Id, &t.Name, &t.Desc, &t.CreateTime, &t.UpdateTime, &contentByte)
AssertError("scan", err)
err = json.Unmarshal(contentByte, content)
AssertError("unmarshal", err)
t.Content = content
tpls = append(tpls, t)
}
for _, t := range tpls {
fmt.Printf("query result; %#v, contnet:%#v\n", t, t.Content)
}
}
import (
"database/sql"
"encoding/json"
"fmt"
"time"
_ "github.com/alexbrainman/odbc"
)
/*
DROP TABLE IF EXISTS template;
CREATE TABLE template
(
id serial(499) NOT NULL PRIMARY KEY,
name varchar(64) NOT NULL,
descript VARCHAR(255) NOT NULL DEFAULT '',
create_time timestamp NOT NULL,
update_time timestamp NOT NULL,
content text DEFAULT NULL
);
*/
const (
DSN = "table_name"
PWD = "password"
ODBC = "odbc"
)
func NowTime() string {
return time.Now().Format(time.DateTime)
}
func AssertError(msg string, err error) {
if err != nil {
panic(fmt.Errorf("msg:%s, err:%s", msg, err))
}
}
type EnTemplate struct {
Id int `json:"id"`
Name string `json:"name"`
Desc string `json:"descript"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
Content *Content `json:"content"`
}
type Content struct {
V1 string `json:"v11111111111111111"`
V2 bool `json:"v22222222222222222"`
V3 int `json:"v33333333333333333"`
V4 int `json:"v4_measurement_measurement_measurement"`
V5 string `json:"v555555555555555555"`
V6 int `json:"v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V7 int `json:"v7v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V8 string `json:"v8v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V9 string `json:"v9v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V10 string `json:"v10v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V11 interface{} `json:"v11v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V12 string `json:"v12v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V13 string `json:"v13v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V14 bool `json:"v14v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V15 string `json:"v15v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V16 string `json:"v16v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V17 string `json:"v17v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V18 string `json:"v18v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V19 int `json:"v19v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V20 string `json:"v20v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V21 string `json:"v21v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V22 string `json:"v22v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V23 string `json:"v23v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V24 string `json:"v24v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V25 string `json:"v25v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V26 string `json:"v26v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V27 string `json:"v27v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V28 string `json:"v28v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V29 int64 `json:"v29v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
V30 string `json:"v30v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V31 string `json:"v31v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V32 string `json:"v32v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V33 string `json:"v33v6_DiagRecord_DiagRecord_DiagRecord_columns"`
V34 bool `json:"v34v6_DiagRecord_DiagRecord_DiagRecord_columns,omitempty"`
}
var prepareData = &Content{
V1: "Hello_world",
V2: false,
V3: 1000,
V4: 2000,
V5: "Hello_world",
V6: 20000,
V7: 80000,
V8: "Hello_world",
V9: "Hello_world",
V10: "Hello_world",
V11: nil,
V12: "Hello_world",
V13: "Hello_world",
V14: false,
V15: "none",
V16: "1",
V17: "Hello_world",
V18: "Hello_world",
V19: -1,
V20: "Hello_world",
V21: "-1",
V22: "-1",
V23: "default",
V24: "-1",
V25: "0",
V26: "Auto",
V27: "Hello_world",
V28: "Hello_world",
V29: 0,
V30: "34",
V31: "2",
V32: "64",
V33: "24",
V34: false,
}
func main() {
dbSourceName := fmt.Sprintf("DSN=%s;UID=gbasedbt;PWD=%s;CHARSET=zh_CN.utf8;", DSN, PWD)
conn, err := sql.Open(ODBC, dbSourceName)
AssertError("open", err)
b, err := json.Marshal(prepareData)
AssertError("marshal", err)
query := "INSERT INTO template(name, descript, create_time, update_time, content) values(?, ?, ?, ?, ?)"
args := []interface{}{
"test", "", NowTime(), NowTime(), string(b),
}
rest, err := conn.Exec(query, args...)
AssertError("exec", err)
fmt.Printf("insert success; rest:%#v\n", rest)
// my query
var tpls []*EnTemplate
query = "SELECT id, name, descript, create_time, update_time, content FROM template ORDER BY id DESC LIMIT 10 OFFSET 0"
rows, err := conn.Query(query)
AssertError("query", err)
defer rows.Close()
for rows.Next() {
t := &EnTemplate{}
var contentByte []byte
var content = &Content{}
err = rows.Scan(&t.Id, &t.Name, &t.Desc, &t.CreateTime, &t.UpdateTime, &contentByte)
AssertError("scan", err)
err = json.Unmarshal(contentByte, content)
AssertError("unmarshal", err)
t.Content = content
tpls = append(tpls, t)
}
for _, t := range tpls {
fmt.Printf("query result; %#v, contnet:%#v\n", t, t.Content)
}
}
小龙虾发表于 2年前
@小龙虾:当我执行上面的代码就会报错 [GBasedbt][GBasedbt ODBC Driver]Unspecified System Error = -11003.
在实际项目中,我的Content会更大,变量名更长一些
我看到插入数据是成功的

在实际项目中,我的Content会更大,变量名更长一些
我看到插入数据是成功的

liaosnet发表于 2年前
你是在 SQLGetData时出现的错误。
For SQLGetData(), the function returned SQL_SUCCESS_WITH_INFO, which means
that the function completed successfully, and that a nonfatal error occurred.
The nonfatal error occurred because the function could not retrieve all the
data for the specified column in one call. The pcbValue argument contains
the length of the data that was left in the column before the current call
to SQLGetData(). Call SQLGetData() repeatedly until all data is retrieved.
For SQLGetData(), the function returned SQL_SUCCESS_WITH_INFO, which means
that the function completed successfully, and that a nonfatal error occurred.
The nonfatal error occurred because the function could not retrieve all the
data for the specified column in one call. The pcbValue argument contains
the length of the data that was left in the column before the current call
to SQLGetData(). Call SQLGetData() repeatedly until all data is retrieved.
最佳回答
GBase用户28017发表于 5个月前
解决没。
山佳发表于 2个月前
等待有缘人解答。
热门帖子
- 12025-12-01浏览数:182759
- 22023-05-09浏览数:25044
- 42023-09-25浏览数:18519
- 52020-05-11浏览数:17526
import (
"database/sql"
"fmt"
_ "github.com/alexbrainman/odbc"
)
func main() {
fmt.Printf("%s\n\n", "GO ODBC测试程序开始运行.")
conn, err := sql.Open("odbc", "DSN=testdb;UID=gbasedbt;PWD=GBase123$%;")
if err != nil {
fmt.Println("链接错误", err)
}
fmt.Printf("%s\n\n", "删除表")
stmt1 , err := conn.Prepare("drop table if exists tabtext")
drop , err := stmt1.Exec()
if err != nil {
fmt.Println("删除表错误", err)
}
_ = drop
fmt.Printf("%s\n\n", "创建表")
stmt11 , err := conn.Prepare("create table tabtext(col1 int, col2 varchar(255), col3 text)")
create , err := stmt11.Exec()
if err != nil {
fmt.Println("创建表错误", err)
}
_ = create
fmt.Printf("%s\n\n", "插入数据")
str := "测试数据类型"
stmt2 , err := conn.Prepare("insert into tabtext values(?,?,?) ")
// insert2 , err := stmt2.Exec(1,"test001",[]byte(str))
insert2, err := stmt2.Exec(1,"test001",str)
if err != nil {
fmt.Println("插入数据错误", err)
}
_ = insert2
fmt.Printf("%s\n\n", "查询数据")
stmt4, err := conn.Prepare("select * from tabtext")
row , err := stmt4.Query()
if err != nil {
fmt.Println("查询数据错误", err)
}
fmt.Printf("%s\n", "数据集显示")
for row.Next() {
var col1 int
var col2 string
var col3 string
if err := row.Scan(&col1,&col2,&col3,); err == nil {
fmt.Printf("%d\t%s\t%s\n",col1,col2,col3)
}
}
fmt.Printf("\n%s\n", "GO ODBC测试程序结束运行.")
return
}
执行,返回结果:
GO ODBC测试程序开始运行.
删除表
创建表
插入数据
查询数据
数据集显示
1 test001 测试数据类型
GO ODBC测试程序结束运行.