database/sql/driver
包定义了 database/sql
包使用的数据库程序要实现的接口
database/sql
包提供了有关 SQL 的数据库通用接口. sql 包必须与数据库驱动程序一起使用.相关程序列表,参见 https://github.com/golang/go/wiki/SQLDrivers
如连接 mysql 需要使用导入 mysql 的驱动程序包
1 2 3 4
| import "database/sql" import _ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql", "user:password@/dbname")
|
常用类型定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| type ColumnType struct { }
type Conn struct { }
type DB struct { }
type DBStats struct { MaxOpenConnections int
OpenConnections int InUse int Idle int
WaitCount int64 WaitDuration time.Duration MaxIdleClosed int64 MaxLifetimeClosed int64 }
type Result interface { LastInsertId() (int64, error) RowsAffected() (int64, error) }
type Row struct { }
type Rows struct { }
type Stmt struct { }
type Tx struct { }
type TxOptions struct { Isolation IsolationLevel ReadOnly bool }
|
常量及变量
1 2 3 4 5 6 7 8 9 10 11
| const ( LevelDefault IsolationLevel = iota LevelReadUncommitted LevelReadCommitted LevelWriteCommitted LevelRepeatableRead LevelSnapshot LevelSerializable LevelLinearizable )
|
常用函数
sql
包函数
1 2 3 4 5 6 7 8 9 10 11 12
| func Drivers() []string
func Register(name string, driver driver.Driver)
func Open(driverName, dataSourceName string) (*DB, error)
func OpenDB(c driver.Connector) *DB
|
ColumnType
结构体方法
ColumnType
定义了列的类型,它包含以下方法
1 2 3 4 5 6 7 8 9 10 11 12
| func (ci *ColumnType) DatabaseTypeName() string
func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool)
func (ci *ColumnType) Length() (length int64, ok bool)
func (ci *ColumnType) Name() string
func (ci *ColumnType) Nullable() (nullable, ok bool)
func (ci *ColumnType) ScanType() reflect.Type
|
DB
结构体方法
DB
定义了数据库的实例对象.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| func (db *DB) Begin() (*Tx, error)
func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)
func (db *DB) Close() error
func (db *DB) Conn(ctx context.Context) (*Conn, error)
func (db *DB) Driver() driver.Driver
func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
func (db *DB) PingContext(ctx context.Context) error
func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
func (db *DB) SetConnMaxLifetime(d time.Duration)
func (db *DB) SetMaxIdleConns(n int)
func (db *DB) SetMaxOpenConns(n int)
func (db *DB) Stats() DBStats
|
Rows
结构体方法
1 2 3 4 5 6 7 8 9 10
| func (rs *Rows) ColumnTypes() ([]*ColumnType, error)
func (rs *Rows) Columns() ([]string, error)
func (rs *Rows) Next() bool
func (rs *Rows) NextResultSet() bool
func (rs *Rows) Scan(dest ...interface{}) error
|
Stmt
结构体方法
1 2 3 4 5 6 7 8 9 10
| func (s *Stmt) Close() error
func (s *Stmt) Exec(args ...interface{}) (Result, error) func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error)
func (s *Stmt) Query(args ...interface{}) (*Rows, error) func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error) func (s *Stmt) QueryRow(args ...interface{}) *Row func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row
|
Tx
结构体方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| func (tx *Tx) Commit() error
func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
func (tx *Tx) Prepare(query string) (*Stmt, error) func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) func (tx *Tx) QueryRow(query string, args ...interface{}) *Row func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
func (tx *Tx) Rollback() error
func (tx *Tx) Stmt(stmt *Stmt) *Stmt func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import ( "database/sql" "fmt" "github.com/go-sql-driver/mysql" )
func main() { cfg := mysql.Config{ User: "username", Passwd: "password", Net: "tcp", Addr: "10.71.1.27:3306", DBName: "datastream-info", AllowNativePasswords: true, } dataSourceName := cfg.FormatDSN() db, err := sql.Open("mysql", dataSourceName) rows, err := db.Query("select * from province_num") var ( id int name string value int ) for rows.Next() { if err = rows.Scan(&id, &name, &value); err != nil { fmt.Println(err) } fmt.Println(id, name, value) } }
|