Inquiry 是一个 Go 包,它将 CSV 文件转换为内存 SQLite 数据库,还可对 CSV 文件 运行 SQL 语句。
使用 Inquiry 非常简单:您“connect”到 CSV 文件,Inquiry 将返回 a*sql.DB和 a []error。然后,您可以使用返回的执行*sql.DB通常对 SQLite 数据库执行的任何操作。
您可以在连接 CSV 文件时使用或不使用选项(ConnectWithOptions 或 Connect)。使用选项时,您可以指定 CSV 分隔符以及文件是否有标题行。如果不提供选项,Inquiry 会将分隔符默认为逗号,并假定没有标题行。
// main.go
package main
import ( "fmt" "log" "os"
"github.com/sionpixley/inquiry/pkg/inquiry" )
type Example struct { Id int Name string Value float64 }
func main() { // 'errs' is a []error with a cap of 25. If there are no errors, then 'errs' will be nil. csvFile, errs := inquiry.Connect[Example]("example.csv") if errs != nil { for _, e := range errs { log.Println(e.Error()) } os.Exit(1) } // Don't forget to close the database. defer csvFile.Close()
rows, err := csvFile.Query("SELECT * FROM Example WHERE Value > 80 ORDER BY Name ASC;") if err != nil { log.Fatalln(err.Error()) }
for rows.Next() { var example Example err = rows.Scan(&example.Id, &example.Name, &example.Value) if err != nil { log.Fatalln(err.Error()) } fmt.Printf("%d %s %f", example.Id, example.Name, example.Value) fmt.Println() } }
|