Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Daneel-Li opened this issue Aug 16, 2024 · 0 comments

Comments

@Daneel-Li
Copy link

Daneel-Li commented Aug 16, 2024

驱动版本 _ "github.com/taosdata/driver-go/v3/taosWS", 显示指向[email protected]

建表及手动测试都没问题:
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)
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant