github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/vfs/memdb/example_test.go (about)

     1  package memdb_test
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"log"
     7  
     8  	_ "embed"
     9  
    10  	_ "github.com/ncruces/go-sqlite3/driver"
    11  	_ "github.com/ncruces/go-sqlite3/embed"
    12  	"github.com/ncruces/go-sqlite3/vfs/memdb"
    13  )
    14  
    15  //go:embed testdata/test.db
    16  var testDB []byte
    17  
    18  func Example() {
    19  	memdb.Create("test.db", testDB)
    20  
    21  	db, err := sql.Open("sqlite3", "file:/test.db?vfs=memdb")
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  	defer db.Close()
    26  
    27  	_, err = db.Exec(`INSERT INTO users (id, name) VALUES (3, 'rust')`)
    28  	if err != nil {
    29  		log.Fatal(err)
    30  	}
    31  
    32  	rows, err := db.Query(`SELECT id, name FROM users`)
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	defer rows.Close()
    37  
    38  	for rows.Next() {
    39  		var id, name string
    40  		err = rows.Scan(&id, &name)
    41  		if err != nil {
    42  			log.Fatal(err)
    43  		}
    44  		fmt.Printf("%s %s\n", id, name)
    45  	}
    46  	// Output:
    47  	// 0 go
    48  	// 1 zig
    49  	// 2 whatever
    50  	// 3 rust
    51  }