csv

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package csv gives SQLite the csv virtual table of SQLite's csv extension (ext/misc/csv.c), ported to Go:

sqlite.NewConnector(dsn, csv.Option())

Then:

CREATE VIRTUAL TABLE temp.t USING csv(filename='data.csv', header=yes);
SELECT * FROM t;

The arguments:

filename=FILE   read the file FILE (relative to the working directory)
data=TEXT       or read TEXT itself
header=YES|NO   the first row names the columns and is not a row
                (default no: the columns are c0, c1, ...)
columns=N       the rows have N columns (default: as many as the first)
schema=SQL      the CREATE TABLE statement of the columns instead
                (with header=yes the first row is still skipped, which
                csv.c does not do when columns= is given too)

Every value is TEXT, a missing field is NULL, and fields past the last column are left out. The rowid counts the rows from 1. The text is RFC 4180: fields separated by commas, rows by LF or CRLF, a field in double quotes may hold commas, line breaks and doubled double quotes. A UTF-8 BOM at the start is skipped.

Where SQLite's csv.c reads a malformed file on, this one fails the statement: a double quote inside a quoted field that is not doubled, or a quoted field that the file ends in. A table never reads a file named by the schema of a database when it is used from a trigger or a view: it is for SQL the program runs (SQLite's DIRECTONLY). Keep such tables in temp, as SQLite advises.

Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/liustack/sqlite"
	"github.com/liustack/sqlite/ext/csv"
)

func main() {
	c, err := sqlite.NewConnector(":memory:", csv.Option())
	if err != nil {
		panic(err)
	}
	db := sql.OpenDB(c)
	defer db.Close()
	db.SetMaxOpenConns(1) // a temp table is on one connection
	_, err = db.Exec(`CREATE VIRTUAL TABLE temp.people USING csv(data='name,age
Ada,36
Linus,21', header=yes)`)
	if err != nil {
		panic(err)
	}
	var name string
	err = db.QueryRow(`SELECT name FROM people WHERE CAST(age AS INTEGER) > 30`).Scan(&name)
	if err != nil {
		panic(err)
	}
	fmt.Println(name)
}
Output:
Ada

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Option

func Option() sqlite.Option

Option adds the csv module to a connector.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL