This is a creation in Article, where the information may have evolved or changed.
Operation SQL class, there is currently only one query, others can be expanded according to this
Package Qormimport ("Database/sql" "FMT" "Reflect" _ "github.com/go-sql-driver/mysql")//request struct type Qdbproto struct {DB * Sql. Db}var Qdbproto *qdbproto//Constructor func Createdbproto (Driver string, Sqllink string) *qdbproto {Qdbproto = new (Qdbproto) DB, E RR: = SQL. Open (Driver, sqllink) if Err = = Nil {qdbproto.db = Dbreturn Qdbproto} else {return nil}}//query func (P *qdbproto) Select (DESTC Lass interface{}, sqlstr string, arg ... interface{}) ([]interface{}, int) {var RowCount int = 0var rows *sql. Rowsvar Err Errorif arg! = nil {rows, err = p.db. Query (SQLSTR)} else {rows, err = p.db. Query (SQLSTR, arg)}if err! = Nil {fmt. Println (Err. Error ()) return nil, rowcount}defer rows. Close () Result: = Make ([]interface{}, 0) for rows. Next () {Scanarr: = make ([]interface{}, 0) Scanentity: = Make (map[string]interface{}) Destvalue: = reflect. ValueOf (destclass) Desttype: = reflect. TypeOf (destclass) FieldCount: = Destvalue.numfield () for fieldindex: = 0; Fieldindex < FieldCount; fieldindex++ {field: = Destvalue.field (Fieldindex) FIeldtype: = field. Type () Newfieldvalue: = reflect. New (FieldType) Fieldvalue: = Newfieldvalue.interface () Scanarr = append (Scanarr, Fieldvalue) fmt. Println (Desttype.field (Fieldindex). Name) Scanentity[desttype.field (Fieldindex). Name] = fieldvalue}if err: = rows. Scan (Scanarr ...); Err! = Nil {fmt. Println (Err. Error ()) return nil, rowCount} else {result = Append (result, scanentity) Rowcount++}}return result, rowCount}
Use:
rows, rowCount := conn2.Select(automethodNew{}, "select * from automethod")