{"id":13591967,"url":"https://github.com/joho/sqltocsv","last_synced_at":"2025-04-12T18:52:45.982Z","repository":{"id":17112439,"uuid":"19878441","full_name":"joho/sqltocsv","owner":"joho","description":"Go package to turn arbitrary database/sql query results into CSV files as easily as possible","archived":false,"fork":false,"pushed_at":"2021-04-28T21:11:05.000Z","size":38,"stargazers_count":186,"open_issues_count":5,"forks_count":42,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-10-14T21:05:55.222Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joho.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-17T04:32:15.000Z","updated_at":"2024-09-14T06:50:41.000Z","dependencies_parsed_at":"2022-07-26T01:49:37.096Z","dependency_job_id":null,"html_url":"https://github.com/joho/sqltocsv","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fsqltocsv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fsqltocsv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fsqltocsv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fsqltocsv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joho","download_url":"https://codeload.github.com/joho/sqltocsv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618243,"owners_count":21134200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T16:01:04.232Z","updated_at":"2025-04-12T18:52:45.953Z","avatar_url":"https://github.com/joho.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# sqltocsv [![Build Status](https://travis-ci.org/joho/sqltocsv.svg?branch=master)](https://travis-ci.org/joho/sqltocsv)\n\nA library designed to let you easily turn any arbitrary sql.Rows result from a query into a CSV file with a minimum of fuss. Remember to handle your errors and close your rows (not demonstrated in every example).\n\n## Usage\n\nImporting the package\n\n```go\nimport (\n    \"database/sql\"\n    _ \"github.com/go-sql-driver/mysql\" // or the driver of your choice\n    \"github.com/joho/sqltocsv\"\n)\n```\n\nDumping a query to a file\n\n```go\n// we're assuming you've setup your sql.DB etc elsewhere\nrows, _ := db.Query(\"SELECT * FROM users WHERE something=72\")\n\nerr := sqltocsv.WriteFile(\"~/important_user_report.csv\", rows)\nif err != nil {\n    panic(err)\n}\n```\n\nReturn a query as a CSV download on the world wide web\n\n```go\nhttp.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n    rows, err := db.Query(\"SELECT * FROM users WHERE something=72\")\n    if err != nil {\n        http.Error(w, err, http.StatusInternalServerError)\n        return\n    }\n    defer rows.Close()\n\n    w.Header().Set(\"Content-type\", \"text/csv\")\n    w.Header().Set(\"Content-Disposition\", \"attachment; filename=\\\"report.csv\\\"\")\n\n    sqltocsv.Write(w, rows)\n})\nhttp.ListenAndServe(\":8080\", nil)\n```\n\n`Write` and `WriteFile` should do cover the common cases by the power of _Sensible Defaults™_ but if you need more flexibility you can get an instance of a `Converter` and fiddle with a few settings.\n\n```go\nrows, _ := db.Query(\"SELECT * FROM users WHERE something=72\")\n\ncsvConverter := sqltocsv.New(rows)\n\ncsvConverter.TimeFormat = time.RFC822\ncsvConverter.Headers = append(rows.Columns(), \"extra_column_one\", \"extra_column_two\")\n\ncsvConverter.SetRowPreProcessor(func (columns []string) (bool, []string) {\n    // exclude admins from report\n    // NOTE: this is a dumb example because \"where role != 'admin'\" is better\n    // but every now and then you need to exclude stuff because of calculations\n    // that are a pain in sql and this is a contrived README\n    if columns[3] == \"admin\" {\n      return false, []string{}\n    }\n\n    extra_column_one = generateSomethingHypotheticalFromColumn(columns[2])\n    extra_column_two = lookupSomeApiThingForColumn(columns[4])\n\n    return append(columns, extra_column_one, extra_column_two)\n})\n\ncsvConverter.WriteFile(\"~/important_user_report.csv\")\n```\n\nFor more details on what else you can do to the `Converter` see the [sqltocsv godocs](http://godoc.org/github.com/joho/sqltocsv)\n\n## License\n\n\u0026copy; [John Barton](https://johnbarton.co/) but under MIT (see [LICENSE](LICENSE)) except for fakedb_test.go which I lifted from the Go standard library and is under BSD and I am unsure what that means legally.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoho%2Fsqltocsv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoho%2Fsqltocsv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoho%2Fsqltocsv/lists"}