• Stars
    star
    186
  • Rank 207,316 (Top 5 %)
  • Language
    Go
  • License
    Other
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go package to turn arbitrary database/sql query results into CSV files as easily as possible

sqltocsv Build Status

A 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).

Usage

Importing the package

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql" // or the driver of your choice
    "github.com/joho/sqltocsv"
)

Dumping a query to a file

// we're assuming you've setup your sql.DB etc elsewhere
rows, _ := db.Query("SELECT * FROM users WHERE something=72")

err := sqltocsv.WriteFile("~/important_user_report.csv", rows)
if err != nil {
    panic(err)
}

Return a query as a CSV download on the world wide web

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    rows, err := db.Query("SELECT * FROM users WHERE something=72")
    if err != nil {
        http.Error(w, err, http.StatusInternalServerError)
        return
    }
    defer rows.Close()

    w.Header().Set("Content-type", "text/csv")
    w.Header().Set("Content-Disposition", "attachment; filename=\"report.csv\"")

    sqltocsv.Write(w, rows)
})
http.ListenAndServe(":8080", nil)

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.

rows, _ := db.Query("SELECT * FROM users WHERE something=72")

csvConverter := sqltocsv.New(rows)

csvConverter.TimeFormat = time.RFC822
csvConverter.Headers = append(rows.Columns(), "extra_column_one", "extra_column_two")

csvConverter.SetRowPreProcessor(func (columns []string) (bool, []string) {
    // exclude admins from report
    // NOTE: this is a dumb example because "where role != 'admin'" is better
    // but every now and then you need to exclude stuff because of calculations
    // that are a pain in sql and this is a contrived README
    if columns[3] == "admin" {
      return false, []string{}
    }

    extra_column_one = generateSomethingHypotheticalFromColumn(columns[2])
    extra_column_two = lookupSomeApiThingForColumn(columns[4])

    return append(columns, extra_column_one, extra_column_two)
})

csvConverter.WriteFile("~/important_user_report.csv")

For more details on what else you can do to the Converter see the sqltocsv godocs

License

© John Barton but under MIT (see 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.

More Repositories

1

godotenv

A Go port of Ruby's dotenv library (Loads environment variables from .env files)
Go
7,974
star
2

7XX-rfc

An RFC for a new series of HTTP status codes covering developer fouls.
Makefile
4,477
star
3

awesome-code-review

An "Awesome" list of code review resources - articles, papers, tools, etc
4,299
star
4

pico8-vscode

A vscode extension for working with pico-8 cartridges.
TypeScript
36
star
5

dotfiles

Stuff for my terminal
Vim Script
30
star
6

aws-pony

For people who like ponies or poor AWS key hygiene. I think those are the same things.
JavaScript
23
star
7

geonames-rails

a lightweight plugin to map to imported geonames data with a repeatable import
Ruby
18
star
8

lazy-loader

Keeps controllers clean in the face of ugly fragment caching using a lazy load
Ruby
13
star
9

lighthouse-cards

A small sinatra app to generate printable index cards (for agile processes) from lighthouse tickets - now with added dashboard!
JavaScript
9
star
10

absurd-time-extensions

Added some stupid extensions to the time class, such as the swatch "internet time", and the is_beer_oclock? method
Ruby
8
star
11

letour

Watch tour de france highlights without spoilers at http://letour.whoisjohnbarton.com
Go
8
star
12

spacetanks

Railscamp weekend project - fight dracula in space with your tank on the Arduboy
C
6
star
13

mapmaker

A handy DSL for generating google sitemaps
Ruby
5
star
14

blog

My Blog + Homepage
Ruby
5
star
15

sphinx-stemmer-ruby-port

Trying to figure out how much work it is to build our own search engine... by discovering how our current one works.
C
5
star
16

programmers_oblique_strategies

Get some extra nuggets in your fortune cookies
Makefile
5
star
17

shoutcloud_api

ALL CAPS AS A SERVICE. THE CLOUD THAT SHOUTS BACK.
Go
4
star
18

rspec_xml_validation_matchers

Xml validation rspec matcher
Ruby
3
star
19

noobnightexamples

examples for live coding on the go noob night
Go
3
star
20

shoutcloud.io

CSS
3
star
21

alphakey

Handy little library for converting ints to short strings (for things like url shorteners and the like)
Go
3
star
22

seven-languages

repo to hold all my WIP as I work my way through the 7 Languages in 7 Weeks book
Scala
2
star
23

worldpurplerainhour

It's just like earth hour, but instead of turning off the lights, you crank purple rain
Ruby
2
star
24

experiments

scratchpad of random ideas i hack around with in go
Go
2
star
25

pico8

A monorepo for my whole PICO-8 workspace
2
star
26

goadventure

I'm trying to write a twitter based text adventure in go. It may or may not get finished.
Go
2
star
27

jrb

A placeholder repo for my future world dominating ruby framework.
Ruby
2
star
28

joho.github.com

my pages
2
star
29

php

My Personal Home Page, or PHP for short.
HTML
2
star
30

das_downloader

A downloader for destroy all software episodes written in go. A half finished hack at best.
Go
2
star
31

joho

Public README repo
1
star
32

wdyk

JavaScript
1
star
33

actually_js

Ruby
1
star
34

shoutcloud_pro_api

THE PRO VERSION OF THE SUPERIOR WEBSCALE ALL CAPS SOFTWARE AS A SERVICE
Go
1
star
35

gogogpm

A hilariously pointless go wrapper of gpm
Go
1
star
36

prohttphandler

A (not very) pro http handler for go (does simple asset serving and exact match paths -> handler funcs)
Go
1
star
37

test-comply

1
star
38

shirts

Ruby
1
star
39

twitter-bomb-disposal

Delete all your tweets that don't do numbers
TypeScript
1
star
40

snackjs-flickr-gallery

A Flickr image gallery built using SnackJS and CSS3 Transitions
JavaScript
1
star