-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
103 lines (87 loc) · 2.03 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package xbase_test
import (
"fmt"
"github.com/tsingsun/xbase"
"time"
)
// ExampleXBase_Create demonstrates how to create a new DBF file using the xbase package.
func ExampleXBase_Create() {
// Create a new XBase instance
db, _ := xbase.New()
// Add fields to the DBF file
db.AddField("NAME", "C", 30)
db.AddField("SALARY", "N", 9, 2)
db.AddField("BDATE", "D")
// Set the code page
db.SetCodePage(1251)
// Create the DBF file
db.CreateFile("persons.dbf")
// Always check for errors
if db.Error() != nil {
fmt.Println(db.Error())
return
}
// Close the file when done
defer db.Close()
// Add a record
db.Add()
db.SetFieldValue(1, "John Smith")
db.SetFieldValue(2, 1234.56)
db.SetFieldValue(3, time.Date(1998, 2, 20, 0, 0, 0, 0, time.UTC))
db.Save()
// Always check for errors
if db.Error() != nil {
fmt.Println(db.Error())
}
}
// ExampleXBase_Read demonstrates how to read a DBF file using the xbase package.
func ExampleXBase_Read() {
// Open the DBF file
db, err := xbase.Open("persons.dbf", true)
if err != nil {
fmt.Println(err)
return
}
// Always check for errors
if db.Error() != nil {
fmt.Println(db.Error())
return
}
// Close the file when done
defer db.Close()
// Read the records
db.First()
for !db.EOF() {
name := db.FieldValueAsString(1)
salary := db.FieldValueAsFloat(2)
bDate := db.FieldValueAsDate(3)
fmt.Println(name, salary, bDate)
db.Next()
}
}
// ExampleXBase_Append demonstrates how to append a record to a DBF file using the xbase package.
func ExampleXBase_Append() {
// Open the DBF file
db, err := xbase.Open("persons.dbf", false)
if err != nil {
fmt.Println(err)
return
}
// Always check for errors
if db.Error() != nil {
fmt.Println(db.Error())
return
}
// Close the file when done
defer db.Close()
// Append a record
db.Add()
db.SetFieldValue(1, "John Smith")
db.SetFieldValue(2, 1234.56)
db.SetFieldValue(3, time.Date(1998, 2, 20, 0, 0, 0, 0, time.UTC))
db.Save()
// Always check for errors
if db.Error() != nil {
fmt.Println(db.Error())
}
}