github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/vfs/readervfs/example_test.go (about) 1 package readervfs_test 2 3 import ( 4 "database/sql" 5 "fmt" 6 "log" 7 "strings" 8 9 _ "embed" 10 11 _ "github.com/ncruces/go-sqlite3/driver" 12 _ "github.com/ncruces/go-sqlite3/embed" 13 "github.com/ncruces/go-sqlite3/util/ioutil" 14 "github.com/ncruces/go-sqlite3/vfs/readervfs" 15 "github.com/psanford/httpreadat" 16 ) 17 18 //go:embed testdata/test.db 19 var testDB string 20 21 func Example_http() { 22 readervfs.Create("demo.db", httpreadat.New("https://sanford.io/demo.db")) 23 defer readervfs.Delete("demo.db") 24 25 db, err := sql.Open("sqlite3", "file:demo.db?vfs=reader") 26 if err != nil { 27 log.Fatal(err) 28 } 29 defer db.Close() 30 31 magname := map[int]string{ 32 3: "thousand", 33 6: "million", 34 9: "billion", 35 } 36 rows, err := db.Query(` 37 SELECT period, data_value, magntude, units FROM csv 38 WHERE period > '2010' 39 LIMIT 10`) 40 if err != nil { 41 log.Fatal(err) 42 } 43 defer rows.Close() 44 45 for rows.Next() { 46 var period, units string 47 var value int64 48 var mag int 49 err = rows.Scan(&period, &value, &mag, &units) 50 if err != nil { 51 log.Fatal(err) 52 } 53 fmt.Printf("%s: %d %s %s\n", period, value, magname[mag], units) 54 } 55 // Output: 56 // 2010.03: 17463 million Dollars 57 // 2010.06: 17260 million Dollars 58 // 2010.09: 15419 million Dollars 59 // 2010.12: 17088 million Dollars 60 // 2011.03: 18516 million Dollars 61 // 2011.06: 18835 million Dollars 62 // 2011.09: 16390 million Dollars 63 // 2011.12: 18748 million Dollars 64 // 2012.03: 18477 million Dollars 65 // 2012.06: 18270 million Dollars 66 } 67 68 func Example_embed() { 69 readervfs.Create("test.db", ioutil.NewSizeReaderAt(strings.NewReader(testDB))) 70 defer readervfs.Delete("test.db") 71 72 db, err := sql.Open("sqlite3", "file:test.db?vfs=reader") 73 if err != nil { 74 log.Fatal(err) 75 } 76 defer db.Close() 77 78 rows, err := db.Query(`SELECT id, name FROM users`) 79 if err != nil { 80 log.Fatal(err) 81 } 82 defer rows.Close() 83 84 for rows.Next() { 85 var id, name string 86 err = rows.Scan(&id, &name) 87 if err != nil { 88 log.Fatal(err) 89 } 90 fmt.Printf("%s %s\n", id, name) 91 } 92 // Output: 93 // 0 go 94 // 1 zig 95 // 2 whatever 96 }