sql

package module
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: GPL-3.0 Imports: 9 Imported by: 47 modules, 114 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.

README

sqlite

A pure-go wrapper for database/sql using logoove/sqlite

Usage

1. Replace modified sqlite lib in your go.mod
replace modernc.org/sqlite => github.com/fumiama/sqlite3 v1.29.10-simp

replace modernc.org/libc => github.com/fumiama/libc v0.0.0-20240530081950-6f6d8586b5c5
2. Use it
type row struct {
    ID   int // pk
    Name string
}

db := &sql.Sqlite{DBPath: "demo.db"}
err := db.Open(time.Hour)
if err != nil {
    panic(err)
}

err = db.Create("demotable", &row{})
if err != nil {
    panic(err)
}

err = db.Insert("demotable", &row{ID: 123, Name: "Anna"})
if err != nil {
    panic(err)
}

var r row
err = db.Find("demotable", &r, "WHERE ID=123")
if err != nil {
    panic(err)
}
fmt.Println(r)

Documentation

Overview

Package sql 数据库/数据处理相关工具

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilDB      = errors.New("sqlite: db is not initialized")
	ErrNullResult = errors.New("sqlite: null result")
	DriverName    = "sqlite3"
)

Functions

func Find added in v1.6.1

func Find[T any](db *Sqlite, table string, condition string, questions ...any) (obj T, err error)

Find 查询数据库,返回第一条结果. condition 可为"WHERE id = 0". 默认字段与结构体元素顺序一致. 返回错误.

func FindAll added in v1.6.1

func FindAll[T any](db *Sqlite, table string, condition string, questions ...any) ([]*T, error)

FindAll 查询数据库,返回多个结果. condition 可为"WHERE id = 0". 默认字段与结构体元素顺序一致. 返回错误.

func Query added in v1.6.1

func Query[T any](db *Sqlite, q string, args ...any) (obj T, err error)

Query 查询数据库,返回第一条结果. q 为一整条查询语句, 慎用. 默认字段与结构体元素顺序一致. 返回错误.

func QueryAll added in v1.6.1

func QueryAll[T any](db *Sqlite, q string, questions ...any) ([]*T, error)

QueryAll 查询数据库,返回多个结果. q 为一整条查询语句, 慎用. 默认字段与结构体元素顺序一致. 返回错误.

func QuerySet added in v1.7.1

func QuerySet[T any](q, op string, s []T) (string, []any)

QuerySet returns "q op (?,?,...,?)", []T

Types

type Sqlite

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

Sqlite 数据库对象

func New added in v1.7.0

func New(dbpath string) Sqlite

func (*Sqlite) CanFind

func (db *Sqlite) CanFind(table string, condition string, questions ...any) bool

CanFind 查询数据库是否有 condition. condition 可为"WHERE id = 0". 默认字段与结构体元素顺序一致. 返回错误.

func (*Sqlite) CanQuery added in v0.5.0

func (db *Sqlite) CanQuery(q string, questions ...any) bool

CanQuery 查询数据库是否有 q. q 为一整条查询语句, 慎用. 默认字段与结构体元素顺序一致. 返回错误.

func (*Sqlite) Close

func (db *Sqlite) Close() (err error)

Close 关闭数据库

func (*Sqlite) Count

func (db *Sqlite) Count(table string) (num int, err error)

Count 查询数据库行数. 返回行数以及错误.

func (*Sqlite) Create

func (db *Sqlite) Create(table string, objptr any, additional ...string) (err error)

Create 生成数据库. 默认结构体的第一个元素为主键. 返回错误.

func (*Sqlite) Del

func (db *Sqlite) Del(table string, condition string, questions ...any) error

Del 删除数据库表项. condition 可为"WHERE id = 0". 返回错误.

func (*Sqlite) Drop added in v0.3.0

func (db *Sqlite) Drop(table string) error

Drop 删除数据库表

func (*Sqlite) Exec added in v1.7.2

func (db *Sqlite) Exec(query string, args ...any) (sql.Result, error)

Exec wrap of (*sql.DB).Exec for PRAGMA settings

func (*Sqlite) Find

func (db *Sqlite) Find(table string, objptr any, condition string, questions ...any) error

Find 查询数据库,写入第一条结果到 objptr. condition 可为"WHERE id = 0". 默认字段与结构体元素顺序一致. 返回错误.

func (*Sqlite) FindFor

func (db *Sqlite) FindFor(table string, objptr any, condition string, f func() error, questions ...any) error

FindFor 查询数据库,用函数 f 遍历结果. condition 可为"WHERE id = 0". 默认字段与结构体元素顺序一致. 返回错误.

func (*Sqlite) Insert

func (db *Sqlite) Insert(table string, objptr any) error

Insert 插入数据集. 如果 PK 存在会覆盖. 默认结构体的第一个元素为主键. 返回错误.

func (*Sqlite) InsertUnique

func (db *Sqlite) InsertUnique(table string, objptr any) error

InsertUnique 插入数据集. 如果 PK 存在会报错. 默认结构体的第一个元素为主键. 返回错误.

func (*Sqlite) ListTables

func (db *Sqlite) ListTables() (s []string, err error)

ListTables 列出所有表名 返回所有表名+错误

func (*Sqlite) Open

func (db *Sqlite) Open(cachettl time.Duration) (err error)

Open 打开数据库

func (*Sqlite) Pick

func (db *Sqlite) Pick(table string, objptr any, questions ...any) error

Pick 从 table 随机一行

func (*Sqlite) PickFor added in v1.6.1

func (db *Sqlite) PickFor(table string, n uint, objptr any, f func() error, questions ...any) error

PickFor 从 table 随机多行

func (*Sqlite) Query added in v0.5.0

func (db *Sqlite) Query(q string, objptr any, args ...any) error

Query 查询数据库,写入第一条结果到 objptr. q 为一整条查询语句, 慎用. 默认字段与结构体元素顺序一致. 返回错误.

func (*Sqlite) QueryFor added in v0.5.0

func (db *Sqlite) QueryFor(q string, objptr any, f func() error, questions ...any) error

QueryFor 查询数据库,用函数 f 遍历结果. q 为一整条查询语句, 慎用. 默认字段与结构体元素顺序一致. 返回错误.

Jump to

Keyboard shortcuts

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