dbo

package module
v0.0.0-...-b5c9fba Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

go-mysql

对于 MySQL 数据库常用操作的封闭函数

Requires Go >= 1.10 and MySQL >= 4.1

Package dbi

functions
  • add insert a new record
  • exe update of delete
  • row get one record from result
  • all get all result from query
Parameters
  • query : sql for preparing a statement
  • args : sql parameters
Examples
dbo.MyDB, err = sql.Open("mysql", config.MysqlDSN)
if err != nil {
    panic(err.Error())
}
//defer dbo.MyDB.Close()
dbo.MyDB.SetMaxOpenConns(config.MysqlMaxConn)
dbo.MyDB.SetMaxIdleConns(config.MysqlMaxIdle)
dbo.MyDB.Ping()

var new_id int32 = dbo.add("INSERT INTO test( b ) VALUES( ? )", 1)

row, err := dbo.Row("SELECT * FROM testA WHERE id=?", 2019)
if err != nil {
    panic(err.Error())
} else {
    fmt.Println("find record: ", row["id"])
}

Notes

About Time

A zero time.Time argument to Query/Exec is treated as a MySQL zero timestamp (0000-00-00 00:00:00). A MySQL zero timestamp is returned as a Go zero time.

Timestamps in MySQL are assumed to be in UTC. time.Time arguments are stored as UTC and returned as UTC.

Character Set

Strings are by default UTF-8 encoded in the MySQL connection; they are automatically converted by the MySQL server as needed.

Installation

go get github.com/james0633/go-mysql

Usage

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "github.com/james0633/go-mysql"
)

func main() {
    dbo.MyDB, err := sql.Open("mysql", "mysql://gotest:gomysql@localhost/test")
    ...
}

Testing

mysql@localhost> grant all on test.* to gotest@localhost;
mysql@localhost> grant all on test.* to gotest@localhost identified by 'gomysql';

$ go test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MyDB *sql.DB

Functions

func Add

func Add(query string, args ...interface{}) (int64, error)

add new record to table

func All

func All(query string, args ...interface{}) ([]map[string]string, error)

get all records from query result

func Exe

func Exe(query string, args ...interface{}) (int64, error)

execute commands

func Row

func Row(query string, args ...interface{}) (map[string]string, error)

get one record from query result

Types

This section is empty.

Jump to

Keyboard shortcuts

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