使用oracle数据库插入date类型数据,golang实现
时间: 2023-08-06 22:01:42 浏览: 270
可以使用Oracle提供的go库 "github.com/godror/godror" 来实现。
以下是一个示例程序,用于向Oracle数据库中的表插入日期数据:
```go
package main
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/godror/godror"
)
func main() {
// 创建一个Oracle数据库连接
dsn := godror.ConnectionParams{
Username: "username",
Password: "password",
SID: "sid",
}
db, err := sql.Open("godror", dsn.StringWithPassword())
if err != nil {
panic(err)
}
defer db.Close()
// 准备Insert语句
query := `INSERT INTO my_table (id, my_date) VALUES (:1, :2)`
stmt, err := db.PrepareContext(context.Background(), query)
if err != nil {
panic(err)
}
defer stmt.Close()
// 准备要插入的日期数据
myDate := time.Date(2021, 10, 1, 0, 0, 0, 0, time.UTC)
// 执行Insert语句
result, err := stmt.ExecContext(context.Background(), 1, myDate)
if err != nil {
panic(err)
}
// 输出Insert结果
rowsAffected, err := result.RowsAffected()
if err != nil {
panic(err)
}
fmt.Printf("%d rows affected\n", rowsAffected)
}
```
在上面的示例程序中,我们使用 `time.Date` 函数创建了一个日期对象,然后将其作为参数插入到Insert语句中。在执行Insert语句时,我们使用了 `stmt.ExecContext` 函数,该函数的第二个参数是要插入的日期数据。
需要注意的是,Oracle数据库中的date类型数据与golang中的time.Time类型数据具有不同的格式。在这种情况下,godror库会自动将golang的time.Time类型数据转换为Oracle的date类型数据。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)