github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/core/README.md (about) 1 Core is a lightweight wrapper of sql.DB. 2 3 # Open 4 ```Go 5 db, _ := core.Open(db, connstr) 6 ``` 7 8 # SetMapper 9 ```Go 10 db.SetMapper(SameMapper()) 11 ``` 12 13 ## Scan usage 14 15 ### Scan 16 ```Go 17 rows, _ := db.Query() 18 for rows.Next() { 19 rows.Scan() 20 } 21 ``` 22 23 ### ScanMap 24 ```Go 25 rows, _ := db.Query() 26 for rows.Next() { 27 rows.ScanMap() 28 ``` 29 30 ### ScanSlice 31 32 You can use `[]string`, `[][]byte`, `[]interface{}`, `[]*string`, `[]sql.NullString` to ScanSclice. Notice, slice's length should be equal or less than select columns. 33 34 ```Go 35 rows, _ := db.Query() 36 cols, _ := rows.Columns() 37 for rows.Next() { 38 var s = make([]string, len(cols)) 39 rows.ScanSlice(&s) 40 } 41 ``` 42 43 ```Go 44 rows, _ := db.Query() 45 cols, _ := rows.Columns() 46 for rows.Next() { 47 var s = make([]*string, len(cols)) 48 rows.ScanSlice(&s) 49 } 50 ``` 51 52 ### ScanStruct 53 ```Go 54 rows, _ := db.Query() 55 for rows.Next() { 56 rows.ScanStructByName() 57 rows.ScanStructByIndex() 58 } 59 ``` 60 61 ## Query usage 62 ```Go 63 rows, err := db.Query("select * from table where name = ?", name) 64 65 user = User{ 66 Name:"lunny", 67 } 68 rows, err := db.QueryStruct("select * from table where name = ?Name", 69 &user) 70 71 var user = map[string]interface{}{ 72 "name": "lunny", 73 } 74 rows, err = db.QueryMap("select * from table where name = ?name", 75 &user) 76 ``` 77 78 ## QueryRow usage 79 ```Go 80 row := db.QueryRow("select * from table where name = ?", name) 81 82 user = User{ 83 Name:"lunny", 84 } 85 row := db.QueryRowStruct("select * from table where name = ?Name", 86 &user) 87 88 var user = map[string]interface{}{ 89 "name": "lunny", 90 } 91 row = db.QueryRowMap("select * from table where name = ?name", 92 &user) 93 ``` 94 95 ## Exec usage 96 ```Go 97 db.Exec("insert into user (`name`, title, age, alias, nick_name,created) values (?,?,?,?,?,?)", name, title, age, alias...) 98 99 user = User{ 100 Name:"lunny", 101 Title:"test", 102 Age: 18, 103 } 104 result, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)", 105 &user) 106 107 var user = map[string]interface{}{ 108 "Name": "lunny", 109 "Title": "test", 110 "Age": 18, 111 } 112 result, err = db.ExecMap("insert into user (`name`, title, age, alias, nick_name,created) values (?Name,?Title,?Age,?Alias,?NickName,?Created)", 113 &user) 114 ```