goSqlHelper

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

GoSqlHelper

GoSqlHelper is a go library to help you to execute Sql on mysql,you can easy to query a map[string]interface{} from this library.

Usage

download source:

git clone [email protected]:bobby96333/GoSqlHelper.git

or

go get github.com/bobby96333/GoShellHelper

Easy to use query result,HelperRow is a map[string]interface{} struct

type HelperRow map[string] interface{}

use it

fmt.println(row.ToJson())
fmt.Println("get string:",row.String("col2"))
    
//query a integer
fmt.Println("get Int:",row.PInt("col1"))
//or
if col1,err:=row.Int("col1");err!=nil {
    fmt.Println("query col 1 :",col1)
}

//query a long
fmt.Println("get Int:",row.PInt64("col1"))
//or
if col1,err:=row.Int64("col1");err!=nil {
    fmt.Println("query col 1 :",col1)
}

Demo

Query a row data

package main

import (
	"fmt"
	"github.com/bobby96333/goSqlHelper"
)

func main(){
	fmt.Println("hello")
	conn,err :=goSqlHelper.MysqlOpen("user:password@tcp(127.0.0.1:3306)/dbname")
	checkErr(err)
	row,err := conn.QueryRow("select * from table where col1 = ? and  col2 = ?","123","abc")
	checkErr(err)
	if *row==nil {
		fmt.Println("no found row")
	}else{
		fmt.Printf("%+v",row)
	}
}

func checkErr(err error){
	if err!=nil {
		panic(err)
	}
}

output:

&map[col1:abc col2:123]

Query multi-row data

  rows,err := conn.QueryRows("select * from table where col1 = ? and  col2 = ?","123","abc")
	errCheck(err)
	for _,row :=range *rows {
		fmt.Println("row:",row.ToJson())
	}

Query a big data

  querying,err := conn.Querying("select * from table where col1 = ? and  col2 = ?","123","abc")
	errCheck(err)
	for row,err:=querying.QueryRow();row!=nil&&err==nil;row,err=querying.QueryRow() {
		fmt.Println("row:",row.ToJson())
	}

update or delete sql

  ret,err := conn.Exec("updatetable set col2 = ? where col1 = ? ","abc","123")
	errCheck(err)
	rowNum,err:= ret.RowsAffected()
	errCheck(err)
	fmt.Println("updated row:",rowNum)

Documentation

Index

Constants

View Source
const QUERY_BUFFER_SIZE = 20

Variables

This section is empty.

Functions

func Float64ToStr

func Float64ToStr(val float64) string

*

float64 转 string

func Int32ToStr

func Int32ToStr(val int32) string

*

int64 转 string

func Int64ToStr

func Int64ToStr(val int64) string

*

int64 转 string

func StrToFloat64

func StrToFloat64(val string) (float64, error)

string转float64

func StrToInt32

func StrToInt32(val string) (int32, error)

string转int32

func StrToInt64

func StrToInt64(val string) (int64, error)

string转int64

Types

type HelperRow

type HelperRow map[string]interface{}

func (HelperRow) CleverString

func (this HelperRow) CleverString(key string) string

func (HelperRow) Int

func (this HelperRow) Int(key string) (val *int, err error)

*

int获取key

func (HelperRow) Int64

func (this HelperRow) Int64(key string) (val *int64, err error)

*

int64获取key

func (HelperRow) PInt

func (this HelperRow) PInt(key string) *int

func (HelperRow) PInt64

func (this HelperRow) PInt64(key string) *int64

func (HelperRow) PString

func (this HelperRow) PString(key string) *string

func (HelperRow) String

func (this HelperRow) String(key string) (*string, error)

* 字段串获取key

func (HelperRow) ToJson

func (this HelperRow) ToJson() string

type HelperTable added in v0.0.2

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

func NewTable added in v0.0.2

func NewTable(rows []HelperRow, columns []string) *HelperTable

func (HelperTable) Columns added in v0.0.2

func (this HelperTable) Columns() *[]string

func (HelperTable) Rows added in v0.0.2

func (this HelperTable) Rows() *[]HelperRow

type Querying

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

func NewQuerying

func NewQuerying(rows *sql.Rows) (*Querying, error)

func (*Querying) Close

func (this *Querying) Close()

func (Querying) Columns added in v0.0.2

func (this Querying) Columns() []string

func (Querying) QueryRow

func (this Querying) QueryRow() (HelperRow, error)

type SqlHelper

type SqlHelper struct {
	Connection *sql.DB
}

func MysqlOpen

func MysqlOpen(connectionStr string) (*SqlHelper, error)

func New added in v0.0.2

func New(connectionStr string) (*SqlHelper, error)

func (SqlHelper) Close

func (this SqlHelper) Close() error

关闭连接

func (SqlHelper) Exec

func (this SqlHelper) Exec(sql string, args ...interface{}) (sql.Result, error)

执行sql

func (SqlHelper) Insert

func (this SqlHelper) Insert(sql string, args ...interface{}) (int64, error)

执行插入sql

func (*SqlHelper) Open

func (this *SqlHelper) Open(connectionStr string) error

*

初始化模块

func (SqlHelper) QueryRow

func (this SqlHelper) QueryRow(sql string, args ...interface{}) (HelperRow, error)

*

读取一行

func (SqlHelper) QueryRows

func (this SqlHelper) QueryRows(sql string, args ...interface{}) ([]HelperRow, error)

*

读取多行

func (SqlHelper) QueryScalarInt

func (this SqlHelper) QueryScalarInt(sql string, args ...interface{}) (int, error)

*

读取个值

func (SqlHelper) QueryTable added in v0.0.2

func (this SqlHelper) QueryTable(sql string, args ...interface{}) (*HelperTable, error)

*

读取多行

func (SqlHelper) Querying

func (this SqlHelper) Querying(sql string, args ...interface{}) (*Querying, error)

* get Querying handler

func (*SqlHelper) SetDB

func (this *SqlHelper) SetDB(conn *sql.DB)

* 初始化

func (SqlHelper) UpdateOrDel

func (this SqlHelper) UpdateOrDel(sql string, args ...interface{}) (int64, error)

更新或删除sql

Jump to

Keyboard shortcuts

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