marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
646 stars 97 forks source link

Appender Not working. #251

Closed Vignesh2308m closed 1 month ago

Vignesh2308m commented 1 month ago

I'm using go-duckdb for one of my personal project all dependencies are fine but when i run sample code for appending to table it's not giving me the result. `package main

import ( "context" "database/sql" "fmt"

"github.com/marcboeker/go-duckdb"
_ "github.com/marcboeker/go-duckdb"

)

func main() {

c, err := duckdb.NewConnector("", nil)
if err != nil {
    print(err)
}

con, err := c.Connect(context.Background())
if err != nil {
    print(err)
}

db := sql.OpenDB(c)
db.Exec(`CREATE TABLE person (id INTEGER, name VARCHAR)`)

a, err := duckdb.NewAppenderFromConn(con, "", "person")
if err != nil {
    print(err)
}

a.AppendRow(1, "apple")

var (
    id   int
    name string
)
row := db.QueryRowContext(context.Background(), `SELECT id, name FROM person`)
_ = row.Scan(&id, &name)
fmt.Println("id:", id, "name:", name)

}`

The result was, id: 0 name:

my go version is 1.22.5

marcboeker commented 1 month ago

I would suggest to always check returning errors. The line a.AppendRow(1, "apple") states that it cannot cast int to int32. Defining a column as INTEGER means it is a int32 in Go. But you are passing an int (which is the default for any unspecified integer in Go) to the appender.

When casting it to int32 beforehand, the append works:

if err := a.AppendRow(int32(1), "apple"); err != nil {
  // Handle error
}