Skip to content

Go 驱动使用占位符方式执行语句报错(好像不会自动添加双引号) #282

@Daneel-Li

Description

@Daneel-Li

驱动版本 _ "github.com/taosdata/driver-go/v3/taosWS", 显示指向v3@v3.5.6

建表及手动测试都没问题:
create table demo(_ts timestamp, col1 varchar(16), col2 varchar(16)); // 建表语句
insert into demo values(NOW(), "123","123"); //手动执行成功
insert into demo values(NOW(), "123","abc"); //手动执行成功

但是用代码占位符方式写数据报错:
`
func Test_InsertDemo(t *testing.T) {
cfg := config.LoadConfig("../../../config.json").Taos

var taosUri = fmt.Sprintf("%s:%s@%s(%s)/%s", cfg.Username, cfg.Password, cfg.Protocol, cfg.Address, cfg.DBName)
if len(cfg.Param) > 0 {
	taosUri = fmt.Sprintf("%s?params=%s", taosUri, cfg.Param)
}

db, err := sql.Open("taosWS", taosUri)
if err != nil {
	fmt.Println("failed to connect TDengine, err:", err)
	t.Fail()
}
if err = db.Ping(); err != nil {
	t.Fail()
}

stmt := "INSERT INTO demo values(?,?,?)"
if _, err := db.Exec(stmt, time.Now(), "123", "123"); err != nil { //成功
	fmt.Println("1 exec failed:", err.Error())
} else {
	fmt.Println("1: success")
}

//失败,报错syntax error near 'abc);' (invalid data or symbol) (0.000197s)
// 和手动执行:insert into demo values(NOW(), "123",abc); 报错一样
if _, err := db.Exec(stmt, time.Now(), "123", "abc"); err != nil { //失败,报错syntax error near 'abc);' (invalid data or symbol) (0.000197s)
	fmt.Println("2 exec failed: ", err.Error())
} else {
	fmt.Println("2: success")
}

}
打印结果:
=== RUN Test_InsertDemo
1: success
2 exec failed: [0x216] syntax error near 'abc)' (invalid data or symbol)
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions