Skip to content

Commit

Permalink
Update README to mention the New function
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Apr 9, 2021
1 parent 6581d91 commit 6d2e6f3
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,59 @@ Given, you have a mysql database called **txdb_test** and a table **users** with
column.

``` go
package main
package main

import (
"database/sql"
"log"

"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)

func init() {
// we register an sql driver named "txdb"
txdb.Register("txdb", "mysql", "root@/txdb_test")
}

func main() {
// dsn serves as an unique identifier for connection pool
db, err := sql.Open("txdb", "identifier")
if err != nil {
log.Fatal(err)
}
defer db.Close()

if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
}
```

You can also use [`sql.OpenDB`](https://golang.org/pkg/database/sql/#OpenDB) (added in Go 1.10) rather than registering a txdb driver instance, if you prefer:

``` go
package main

import (
"database/sql"
"log"
import (
"database/sql"
"log"

"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)
"github.com/DATA-DOG/go-txdb"
_ "github.com/go-sql-driver/mysql"
)

func init() {
// we register an sql driver named "txdb"
txdb.Register("txdb", "mysql", "root@/txdb_test")
func main() {
db, err := sql.OpenDB(txdb.New("mysql", "root@/txdb_test"))
if err != nil {
log.Fatal(err)
}
defer db.Close()

func main() {
// dsn serves as an unique identifier for connection pool
db, err := sql.Open("txdb", "identifier")
if err != nil {
log.Fatal(err)
}
defer db.Close()

if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
log.Fatal(err)
}
}
```

Every time you will run this application, it will remain in the same state as before.
Expand Down

0 comments on commit 6d2e6f3

Please sign in to comment.