go-sqlite-lite是Go编程语言的SQLite驱动程序

网友投稿 816 2022-10-17

go-sqlite-lite是Go编程语言的SQLite驱动程序

go-sqlite-lite是Go编程语言的SQLite驱动程序

go-sqlite-lite

go-sqlite-lite is a SQLite driver for the Go programming language. It is designed with the following goals in mind.

Lightweight - Most methods should be little more than a small wrapper around SQLite C functions.Performance - Where possible, methods should be available to allow for the highest performance possible.Understandable - You should always know what SQLite functions are being called and in what order.Unsurprising - Connections, PRAGMAs, transactions, bindings, and stepping should work out of the box exactly as you would expect with SQLite.Debuggable - When you encounter a SQLite error, the SQLite documentation should be relevant and relatable to the Go code.Ergonomic - Where it makes sense, convenient compound methods should exist to make tasks easy and to conform to Go standard interfaces.

Most database drivers include a layer to work nicely with the Go database/sql interface, which introduces connection pooling and behavior differences from pure SQLite. This driver does not include a database/sql interface.

Releases

2019-05-01 v0.6.1 - Bug fixes, authorizer callback support2019-05-01 v0.6.0 - SQLite version 3.28.02019-02-05 v0.5.0 - SQLite version 3.26.02018-10-30 v0.4.2 - Better error messages from SQLite2018-10-11 v0.4.1 - Fixed an issue with new go 1.11 modules2018-09-29 v0.4.0 - SQLite version 3.25.2. Add support for the Session extension2018-09-16 v0.3.1 - Forgot to update sqlite3.h2018-09-16 v0.3.0 - SQLite version 3.25.02018-09-14 v0.2.0 - Proper error and NULL handling on Column* methods. Empty blobs and empty strings are now distinct from NULL in all cases. A nil byte slice is interpreted as NULL for binding purposes as well as Column* methods.2018-09-01 v0.1.2 - Added Column methods to Stmt, and WithTx methods to Conn2018-08-25 v0.1.1 - Fixed linking on some Linux systems2018-08-21 v0.1.0 - SQLite version 3.24.0

Getting started

import "github.com/bvinc/go-sqlite-lite/sqlite3"

Acquiring a connection

conn, err := sqlite3.Open("mydatabase.db")if err != nil { ...}defer conn.Close()// It's always a good idea to set a busy timeoutconn.BusyTimeout(5 * time.Second)

Executing SQL

err = conn.Exec(`CREATE TABLE student(name TEXT, age INTEGER)`)if err != nil { ...}// Exec can optionally bind parameterserr = conn.Exec(`INSERT INTO student VALUES (?, ?)`, "Bob", 18)if err != nil { ...}

Using Prepared Statements

stmt, err := conn.Prepare(`INSERT INTO student VALUES (?, ?)`)if err != nil { ...}defer stmt.Close()// Bind the argumentserr = stmt.Bind("Bill", 18)if err != nil { ...}// Step the statementhasRow, err := stmt.Step()if err != nil { ...}// Reset the statementerr = stmt.Reset()if err != nil { ...}

Using Prepared Statements Conveniently

stmt, err := conn.Prepare(`INSERT INTO student VALUES (?, ?)`)if err != nil { ...}defer stmt.Close()// Exec binds arguments, steps the statement to completion, and always resets the statementerr = stmt.Exec("John", 19)if err != nil { ...}

Using Queries Conveniently

// Prepare can prepare a statement and optionally also bind argumentsstmt, err := conn.Prepare(`SELECT name, age FROM student WHERE age = ?`, 18)if err != nil { ...}defer stmt.Close()for { hasRow, err := stmt.Step() if err != nil { ... } if !hasRow { // The query is finished break } // Use Scan to access column data from a row var name string var age int err = stmt.Scan(&name, &age) if err != nil { ... } fmt.Println("name:", name, "age:", age)}// Remember to Reset the statement if you would like to Bind new arguments and reuse the prepared statement

Getting columns that might be NULL

Scan can be convenient to use, but it doesn't handle NULL values. To get full control of column values, there are column methods for each type.

name, ok, err := stmt.ColumnText(0)if err != nil { // Either the column index was out of range, or SQLite failed to allocate memory ...}if !ok { // The column was NULL}age, ok, err := stmt.ColumnInt(1)if err != nil { // Can only fail if the column index is out of range ...}if !ok { // The column was NULL}

ColumnBlob returns a nil slice in the case of NULL.

blob, err := stmt.ColumnBlob(i)if err != nil { // Either the column index was out of range, or SQLite failed to allocate memory ...}if blob == nil { // The column was NULL}

Using Transactions

// Equivalent to conn.Exec("BEGIN")err := conn.Begin()if err != nil { ...}// Do some work...// Equivalent to conn.Exec("COMMIT")err = conn.Commit()if err != nil { ...}

Using Transactions Conveniently

With error handling in Go, it can be pretty inconvenient to ensure that a transaction is rolled back in the case of an error. The WithTx method is provided, which accepts a function of work to do inside of a transaction. WithTx will begin the transaction and call the function. If the function returns an error, the transaction will be rolled back. If the function succeeds, the transaction will be committed. WithTx can be a little awkward to use, but it's necessary. For example:

err := conn.WithTx(func() error { return insertStudents(conn)})if err != nil { ...}func insertStudents(conn *sqlite3.Conn) error { ...}

Advanced Features

Binding parameters to statements using SQLite named parameters.SQLite Blob Incremental IO API.SQLite Online Backup API.SQLite Session extension.Supports setting a custom busy handlerSupports callback hooks on commit, rollback, and update.Supports setting compile-Time authorization callbacks.If shared cache mode is enabled and one statement receives a SQLITE_LOCKED error, the SQLite unlock_notify extension is used to transparently block and try again when the conflicting statement finishes.Compiled with SQLite support for JSON1, RTREE, FTS5, GEOPOLY, STAT4, and SOUNDEX.Compiled with SQLite support for OFFSET/LIMIT on UPDATE and DELETE statements.RawString and RawBytes can be used to reduce copying between Go and SQLite. Please use with caution.

Credit

This project began as a fork of https://github.com/mxk/go-sqlite/

FAQ

Why is there no database/sql interface?

If a database/sql interface is required, please use https://github.com/mattn/go-sqlite3 . In my experience, using a database/sql interface with SQLite is painful. Connection pooling causes unnecessary overhead and weirdness. Transactions using Exec("BEGIN") don't work as expected. Your connection does not correspond to SQLite's concept of a connection. PRAGMA commands do not work as expected. When you hit SQLite errors, such as locking or busy errors, it's difficult to discover why since you don't know which connection received which SQL and in what order.

What are the differences between this driver and the mxk/go-sqlite driver?

This driver was forked from mxk/go-sqlite-driver. It hadn't been maintained in years and used an ancient version of SQLite. A large number of features were removed, reworked, and renamed. A lot of smartness and state was removed. It is now much easier to upgrade to newer versions of SQLite since the codec feature was removed. The behavior of methods now lines up closely with the behavior of SQLite's C API.

What are the differences between this driver and the crawshaw/sqlite driver?

The crawshaw driver is pretty well thought out and solves a lot of the same problems as this driver. There are a few places where our philosophies differ. The crawshaw driver defaults (when flags of 0 are given) to SQLite shared cache mode and WAL mode. The default WAL synchronous mode is changed. Prepared statements are transparently cached. Connection pools are provided. I would be opposed to making most of these changes to this driver. I would like this driver to provide a default, light, and unsurprising SQLite experience.

Are finalizers provided to automatically close connections and statements?

No finalizers are used in this driver. You are responsible for closing connections and statements. While I mostly agree with finalizers for cleaning up most accidental resource leaks, in this case, finalizers may fix errors such as locking errors while debugging only to find that the code works unreliably in production. Removing finalizers makes the behavior consistent.

Is it thread safe?

go-sqlite-lite is as thread safe as SQLite. SQLite with this driver is compiled with -DSQLITE_THREADSAFE=2 which is Multi-thread mode. In this mode, SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads. This applies to goroutines. A single database connection should not be used simultaneously between two goroutines.

It is safe to use separate connection instances concurrently, even if they are accessing the same database file. For example:

// ERROR (without any extra synchronization)c, _ := sqlite3.Open("sqlite.db")go use(c)go use(c)

// OKc1, _ := sqlite3.Open("sqlite.db")c2, _ := sqlite3.Open("sqlite.db")go use(c1)go use(c2)

Consult the SQLite documentation for more information.

https://sqlite.org/threadsafe.html

How do I pool connections for handling HTTP requests?

Opening new connections is cheap and connection pooling is generally unnecessary for SQLite. I would recommend that you open a new connection for each request that you're handling. This ensures that each request is handled separately and the normal rules of SQLite database/table locking apply.

If you've decided that pooling connections provides you with an advantage, it would be outside the scope of this package and something that you would need to implement and ensure works as needed.

License

This project is licensed under the BSD license.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:手动实现代理服务器(仿nginx)
下一篇:为了 GitHub 问题票,开发者做了一套小票打印机解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~