simplesqlite

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

README

SimpleSqlite (no C)

GoDoc

An easy way to use a SQLite database from Go.

Online API Documentation

godoc.org

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Uses the pure-Go modernc.org/sqlite driver, so CGO is not required.
  • Modeled after simplemaria.
  • The hash maps behaves like hash maps, but are not backed by actual hashmaps, unlike with simpleredis. This is for keeping compatibility with simpleredis. If performance when scaling up is a concern, simpleredis backed by redis might be a better choice.

Sample usage

package main

import (
    "log"

    "github.com/xyproto/simplesqlite_noc"
)

func main() {
    // Check if the simplesqlite is working
    if err := db.TestConnection(); err != nil {
        log.Fatalln("Could not open database file.")
    }

    // Create a new File
    file := db.New()

    // Use another filename
    //file := db.NewFile("sqlite.db")

    // Close the connection when the function returns
    defer file.Close()

    // Create a list named "greetings"
    list, err := db.NewList(file, "greetings")
    if err != nil {
        log.Fatalln("Could not create list!")
    }

    // Add "hello" to the list, check if there are errors
    if list.Add("hello") != nil {
        log.Fatalln("Could not add an item to list!")
    }

    // Get the last item of the list
    if item, err := list.GetLast(); err != nil {
        log.Fatalln("Could not fetch the last item from the list!")
    } else {
        log.Println("The value of the stored item is:", item)
    }

    // Remove the list
    if list.Remove() != nil {
        log.Fatalln("Could not remove the list!")
    }
}

Testing

The tests will create a file (sqlite.db) for go test to work.

Version, license and author

Documentation

Index

Constants

View Source
const (
	// Version number. Stable API within major version numbers.
	Version = 3.2
)

Variables

View Source
var Verbose = false

Functions

func TestConnection

func TestConnection() (err error)

Test if the local database server is up and running.

func TestConnectionFile

func TestConnectionFile(connectionString string) (err error)

Test if a given database server is up and running. connectionString may be on the form "sqlite.db&cache=shared&mode=memory".

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File represents a specific database

func New

func New() *File

The default database connection

func NewFile

func NewFile(connectionString string) *File

Create a new database connection. connectionString may be on the form "sqlite.db&cache=shared&mode=memory".

func (*File) Close

func (file *File) Close()

Close the connection

func (*File) Ping

func (file *File) Ping() error

Ping the file

type HashMap

type HashMap dbDatastructure

func NewHashMap

func NewHashMap(file *File, name string) (*HashMap, error)

Create a new hashmap

func (*HashMap) All

func (h *HashMap) All() ([]string, error)

Get all owners (not keys, not values) for all hash elements

func (*HashMap) Clear

func (h *HashMap) Clear() error

Clear the contents

func (*HashMap) Del

func (h *HashMap) Del(owner string) error

Remove an element (for instance a user)

func (*HashMap) DelKey

func (h *HashMap) DelKey(owner, key string) error

Remove a key for an entry in a hashmap (for instance the email field for a user)

func (*HashMap) Exists

func (h *HashMap) Exists(owner string) (bool, error)

Check if a given owner exists as a hash map at all

func (*HashMap) Get

func (h *HashMap) Get(owner, key string) (string, error)

Get a value from a hashmap given the element id (for instance a user id) and the key (for instance "password").

func (*HashMap) GetAll

func (h *HashMap) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*HashMap) Has

func (h *HashMap) Has(owner, key string) (bool, error)

Check if a given owner + key is in the hash map

func (*HashMap) Keys

func (h *HashMap) Keys(owner string) ([]string, error)

Get all keys for a given owner

func (*HashMap) Remove

func (h *HashMap) Remove() error

Remove this hashmap

func (*HashMap) Set

func (h *HashMap) Set(owner, key, value string) error

Set a value in a hashmap given the element id (for instance a user id) and the key (for instance "password")

type KeyValue

type KeyValue dbDatastructure

func NewKeyValue

func NewKeyValue(file *File, name string) (*KeyValue, error)

Create a new key/value

func (*KeyValue) Clear

func (kv *KeyValue) Clear() error

Clear this key/value

func (*KeyValue) Del

func (kv *KeyValue) Del(key string) error

Remove a key

func (*KeyValue) Get

func (kv *KeyValue) Get(key string) (string, error)

Get a value given a key

func (*KeyValue) Inc

func (kv *KeyValue) Inc(key string) (string, error)

Increase the value of a key, returns the new value Returns an empty string if there were errors, or "0" if the key does not already exist.

func (*KeyValue) Remove

func (kv *KeyValue) Remove() error

Remove this key/value

func (*KeyValue) Set

func (kv *KeyValue) Set(key, value string) error

Set a key and value

type List

type List dbDatastructure

func NewList

func NewList(file *File, name string) (*List, error)

Create a new list. Lists are ordered.

func (*List) Add

func (l *List) Add(value string) error

Add an element to the list

func (*List) All

func (l *List) All() ([]string, error)

Get all elements of a list

func (*List) Clear

func (l *List) Clear() error

Clear the list contents

func (*List) GetAll

func (l *List) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*List) GetLast

func (l *List) GetLast() (string, error)

Deprecated, please use .Last() instead

func (*List) GetLastN

func (l *List) GetLastN(n int) ([]string, error)

Deprecated, please use .LastN(n) instead

func (*List) Last

func (l *List) Last() (string, error)

Get the last element of a list

func (*List) LastN

func (l *List) LastN(n int) ([]string, error)

Get the last N elements of a list

func (*List) Remove

func (l *List) Remove() error

Remove this list

type SQLiteCreator

type SQLiteCreator struct {
	// contains filtered or unexported fields
}

func NewCreator

func NewCreator(file *File) *SQLiteCreator

func (*SQLiteCreator) NewHashMap

func (m *SQLiteCreator) NewHashMap(id string) (pinterface.IHashMap, error)

func (*SQLiteCreator) NewKeyValue

func (m *SQLiteCreator) NewKeyValue(id string) (pinterface.IKeyValue, error)

func (*SQLiteCreator) NewList

func (m *SQLiteCreator) NewList(id string) (pinterface.IList, error)

func (*SQLiteCreator) NewSet

func (m *SQLiteCreator) NewSet(id string) (pinterface.ISet, error)

type Set

type Set dbDatastructure

func NewSet

func NewSet(file *File, name string) (*Set, error)

Create a new set

func (*Set) Add

func (s *Set) Add(value string) error

Add an element to the set

func (*Set) All

func (s *Set) All() ([]string, error)

Get all elements of the set

func (*Set) Clear

func (s *Set) Clear() error

Clear the list contents

func (*Set) Del

func (s *Set) Del(value string) error

Remove an element from the set

func (*Set) GetAll

func (s *Set) GetAll() ([]string, error)

Deprecated, please use .All() instead

func (*Set) Has

func (s *Set) Has(value string) (bool, error)

Check if a given value is in the set

func (*Set) Remove

func (s *Set) Remove() error

Remove this set

Directories

Path Synopsis
cmd
simple command

Jump to

Keyboard shortcuts

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