You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.3 KiB

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