-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe93a2c
commit 209fa2c
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"log" | ||
|
||
"github.com/marcboeker/go-duckdb" | ||
) | ||
|
||
func main() { | ||
c, err := duckdb.NewConnector("", nil) | ||
if err != nil { | ||
log.Fatalf("could not initialize new connector: %s", err.Error()) | ||
} | ||
|
||
con, err := c.Connect(context.Background()) | ||
if err != nil { | ||
log.Fatalf("could not connect: %s", err.Error()) | ||
} | ||
|
||
db := sql.OpenDB(c) | ||
if _, err := db.Exec(`CREATE TABLE users (name VARCHAR, age INTEGER)`); err != nil { | ||
log.Fatalf("could not create table users: %s", err.Error()) | ||
} | ||
|
||
a, err := duckdb.NewAppenderFromConn(con, "", "users") | ||
if err != nil { | ||
log.Fatalf("could not create new appender for users: %s", err.Error()) | ||
} | ||
|
||
if err := a.AppendRow("Fred", int32(34)); err != nil { | ||
log.Fatalf("could not append row to users: %s", err.Error()) | ||
} | ||
|
||
if err := a.Close(); err != nil { | ||
log.Fatalf("could not flush and close appender: %s", err.Error()) | ||
} | ||
|
||
var ( | ||
name string | ||
age int | ||
) | ||
row := db.QueryRowContext(context.Background(), `SELECT name, age FROM users`) | ||
if err := row.Scan(&name, &age); err != nil { | ||
log.Fatalf("could not retrieve user from db: %s", err.Error()) | ||
} | ||
|
||
log.Printf("User: name=%s, age=%d", name, age) | ||
} |