myql操作
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.

905 lines
19 KiB

  1. package tencentdb
  2. import (
  3. "database/sql"
  4. "log"
  5. "strconv"
  6. "errors"
  7. "strings"
  8. "time"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. /**
  12. * 创建数据
  13. */
  14. func Insert(dbName, table string, data map[string]string) (int64, error) {
  15. var insertId int64
  16. var err error
  17. if dbName == "" && table == "" {
  18. return insertId, errors.New("没有数据表")
  19. }
  20. dbName = getTableName(dbName, table)
  21. if len(data) < 1 {
  22. return insertId, errors.New("没有要写入的数据")
  23. }
  24. keyList := make([]string, len(data))
  25. keyStr := make([]string, len(data))
  26. valueList := make([]interface{}, len(data))
  27. var i int = 0
  28. for key, value := range data {
  29. keyList[i] = key
  30. keyStr[i] = "?"
  31. valueList[i] = value
  32. i++
  33. }
  34. result, err := DB.Exec("insert into "+dbName+" ("+strings.Join(keyList, ",")+") value("+strings.Join(keyStr, ",")+")", valueList...)
  35. if err != nil {
  36. log.Println("ERROR|插入", dbName, "数据失败,", err)
  37. return insertId, err
  38. } else {
  39. insertId, _ = result.LastInsertId()
  40. time.Sleep(time.Second * 2)
  41. return insertId, nil
  42. }
  43. }
  44. /**
  45. * 修改数据
  46. */
  47. func Update(dbName, table string, data map[string]string, where map[string]string) (int64, error) {
  48. var rowsAffected int64
  49. var err error
  50. if dbName == "" && table == "" {
  51. return rowsAffected, errors.New("没有数据表")
  52. }
  53. dbName = getTableName(dbName, table)
  54. if len(data) < 1 {
  55. return rowsAffected, errors.New("同有更新的数据")
  56. }
  57. if len(where) < 1 {
  58. return rowsAffected, errors.New("没有更新条件")
  59. }
  60. keyList := make([]string, len(data))
  61. valueList := make([]interface{}, len(data), len(data)+len(where))
  62. whereStr := make([]string, len(where))
  63. var i int = 0
  64. empty := false
  65. for key, value := range data {
  66. keyList[i] = key + "=?"
  67. valueList[i] = value
  68. i++
  69. }
  70. i = 0
  71. for key, value := range where {
  72. if value == "" {
  73. empty = true
  74. break
  75. }
  76. whereStr[i] = key + "=?"
  77. valueList = append(valueList, value)
  78. i++
  79. }
  80. if empty {
  81. log.Println("ERROR|修改数据表", dbName, "时条件中有空数据,条件:", where, "数据:", data)
  82. return rowsAffected, errors.New("条件中有空数据")
  83. }
  84. result, err := DB.Exec("update "+dbName+" set "+strings.Join(keyList, " , ")+" where "+strings.Join(whereStr, " and "), valueList...)
  85. if err != nil {
  86. log.Println("ERROR|修改", dbName, "数据失败,", err)
  87. return rowsAffected, err
  88. } else {
  89. rowsAffected, _ = result.RowsAffected()
  90. return rowsAffected, nil
  91. }
  92. }
  93. /**
  94. * 删除数据
  95. * @param count 删除数量
  96. */
  97. func Delete(dbName, table string, data map[string]string, del_count ...string) (int64, error) {
  98. var count int64
  99. var err error
  100. if dbName == "" && table == "" {
  101. return count, errors.New("没有数据表")
  102. }
  103. dbName = getTableName(dbName, table)
  104. if len(data) < 1 {
  105. return count, errors.New("没有要删除的数据")
  106. }
  107. keyList := make([]string, len(data))
  108. valueList := make([]interface{}, len(data))
  109. var i int = 0
  110. empty := false
  111. for key, value := range data {
  112. if value == "" {
  113. empty = true
  114. break
  115. }
  116. keyList[i] = key + "=?"
  117. valueList[i] = value
  118. i++
  119. }
  120. if empty {
  121. log.Println("ERROR|删除数据表", dbName, "时条件中有空数据,条件:", data)
  122. return count, errors.New("条件中有空数据")
  123. }
  124. var limitStr string = ""
  125. if len(del_count) > 0 {
  126. limitStr = " limit " + del_count[0]
  127. }
  128. result, err := DB.Exec("delete from "+dbName+" where "+strings.Join(keyList, " and ")+limitStr, valueList...)
  129. if err != nil {
  130. log.Println("ERROR|删除", dbName, "数据失败,", err)
  131. return count, err
  132. } else {
  133. count, _ = result.RowsAffected()
  134. return count, nil
  135. }
  136. }
  137. /**
  138. * 查找一条记录
  139. * @param dbName 数据表名
  140. * @param title 查询字段名
  141. */
  142. func GetData(dbName, table string, title string, where map[string]string, limit map[string]string) (int, map[string]string, error) {
  143. var count int = 0
  144. info := make(map[string]string)
  145. if dbName == "" && table == "" {
  146. return count, info, errors.New("没有数据表")
  147. }
  148. dbName = getTableName(dbName, table)
  149. if len(title) < 1 {
  150. return count, info, errors.New("没有指定查询内容")
  151. }
  152. var limitStr string = ""
  153. if limit != nil && len(limit) > 0 {
  154. var from string = "0" //开始
  155. if _, ok := limit["order"]; ok {
  156. limitStr += " order by " + limit["order"]
  157. }
  158. if _, ok := limit["from"]; ok {
  159. from = limit["from"]
  160. }
  161. limitStr += " limit " + from + ",1"
  162. } else {
  163. limitStr = " limit 1"
  164. }
  165. if len(where) < 1 {
  166. return count, info, errors.New("Query condition is empty")
  167. }
  168. keyList := make([]string, len(where))
  169. valueList := make([]interface{}, len(where))
  170. var i int = 0
  171. empty := false
  172. for key, value := range where {
  173. if value == "" {
  174. empty = true
  175. break
  176. }
  177. keyList[i] = key + " = ? "
  178. valueList[i] = value
  179. i++
  180. }
  181. if empty {
  182. return count, info, errors.New("Query condition is empty")
  183. }
  184. var rows *sql.Rows
  185. var err error
  186. var queryNum int = 0
  187. for queryNum < 3 { //如发生错误,继续查询3次,防止数据库连接断开问题
  188. rows, err = DB.Query("SELECT "+title+" FROM "+dbName+" where "+strings.Join(keyList, " and ")+" "+limitStr, valueList...)
  189. if err == nil {
  190. break
  191. } else {
  192. log.Println("ERROR", "Query from", dbName, "failed,", err, "| SELECT "+title+" FROM "+dbName+" where "+strings.Join(keyList, " and ")+" "+limitStr)
  193. time.Sleep(time.Millisecond * 500)
  194. }
  195. queryNum++
  196. }
  197. defer rows.Close()
  198. if err != nil {
  199. return count, info, err
  200. }
  201. columns, _ := rows.Columns()
  202. scanArgs := make([]interface{}, len(columns))
  203. values := make([]interface{}, len(columns))
  204. for i := range values {
  205. scanArgs[i] = &values[i]
  206. }
  207. var index string
  208. var rowerr error
  209. for rows.Next() {
  210. rowerr = rows.Scan(scanArgs...)
  211. if rowerr == nil {
  212. for i, col := range values {
  213. if col != nil {
  214. index = StrFirstToUpper(columns[i])
  215. info[index] = ToString(col)
  216. }
  217. }
  218. count++
  219. } else {
  220. log.Println("ERROR", "rows scan error", rowerr, dbName, keyList, valueList)
  221. }
  222. }
  223. if rowerr != nil {
  224. return count, info, rowerr
  225. }
  226. return count, info, nil
  227. }
  228. /**
  229. * 查找一条记录
  230. * @param dbName 数据表名
  231. * @param title 查询字段名
  232. */
  233. func GetRow(dbconn *sql.DB, dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, debug bool) (int, map[string]string, error) {
  234. var count int = 0
  235. info := make(map[string]string)
  236. if dbName == "" && table_name == "" {
  237. return count, info, errors.New("没有数据表")
  238. }
  239. table := getTableName(dbName, table_name)
  240. var sql_str, title string
  241. if titles != "" {
  242. title = titles
  243. } else {
  244. title = "*"
  245. }
  246. sql_str = StringJoin("select ", title)
  247. if alias != "" {
  248. table = StringJoin(table, " as ", alias)
  249. }
  250. sql_str = StringJoin(sql_str, " from ", table)
  251. if len(join) > 0 {
  252. for _, joinitem := range join {
  253. if len(joinitem) < 2 {
  254. continue
  255. }
  256. if len(joinitem) == 3 {
  257. sql_str = StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  258. } else { //默认左连接
  259. sql_str = StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  260. }
  261. }
  262. }
  263. if len(where) > 0 || len(where_or) > 0 {
  264. sql_str = StringJoin(sql_str, " where ")
  265. }
  266. if len(where) > 0 {
  267. sql_str = StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ")
  268. }
  269. if len(where_or) > 0 {
  270. if len(where) > 0 {
  271. sql_str = StringJoin(sql_str, " or ", strings.Join(where_or, " or "))
  272. } else {
  273. sql_str = StringJoin(sql_str, strings.Join(where_or, " or "))
  274. }
  275. }
  276. if orderby != "" {
  277. sql_str = StringJoin(sql_str, " order by ", orderby)
  278. }
  279. if debug {
  280. log.Println("query sql:", sql_str, valueList)
  281. }
  282. condition_len := 0 //所有条件数
  283. for _, ch2 := range sql_str {
  284. if string(ch2) == "?" {
  285. condition_len++
  286. }
  287. }
  288. if condition_len != len(valueList) {
  289. return 0, nil, errors.New("参数错误,条件值错误")
  290. }
  291. var rows *sql.Rows
  292. var err error
  293. var queryNum int = 0
  294. sql_str = StringJoin(sql_str, " limit 1")
  295. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  296. rows, err = dbconn.Query(sql_str, valueList...)
  297. if err == nil {
  298. break
  299. } else {
  300. log.Println(err)
  301. time.Sleep(time.Millisecond * 500)
  302. }
  303. queryNum++
  304. }
  305. if err != nil {
  306. rows.Close()
  307. return count, info, err
  308. }
  309. columns, _ := rows.Columns()
  310. scanArgs := make([]interface{}, len(columns))
  311. values := make([]interface{}, len(columns))
  312. for i := range values {
  313. scanArgs[i] = &values[i]
  314. }
  315. var index string
  316. var rowerr error
  317. for rows.Next() {
  318. rowerr = rows.Scan(scanArgs...)
  319. if rowerr == nil {
  320. for i, col := range values {
  321. if col != nil {
  322. index = StrFirstToUpper(columns[i])
  323. info[index] = ToString(col)
  324. }
  325. }
  326. count++
  327. } else {
  328. log.Println("ERROR", rowerr)
  329. }
  330. }
  331. rows.Close()
  332. if rowerr != nil {
  333. return count, info, rowerr
  334. }
  335. return count, info, nil
  336. }
  337. /**
  338. * 查找多条记录
  339. * @param dbName 数据表名
  340. * @param title 查询字段名
  341. */
  342. func FetchRows(dbconn *sql.DB, dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, page int, page_size int, debug bool) (int, []map[string]string, error) {
  343. var count int = 0
  344. list := make([]map[string]string, 0)
  345. if dbName == "" && table_name == "" {
  346. return count, list, errors.New("没有数据表")
  347. }
  348. table := getTableName(dbName, table_name)
  349. var sql_str, title string
  350. if titles != "" {
  351. title = titles
  352. } else {
  353. title = "*"
  354. }
  355. sql_str = StringJoin("select ", title)
  356. if alias != "" {
  357. table = StringJoin(table, " as ", alias)
  358. }
  359. sql_str = StringJoin(sql_str, " from ", table)
  360. if len(join) > 0 {
  361. for _, joinitem := range join {
  362. if len(joinitem) < 2 {
  363. continue
  364. }
  365. if len(joinitem) == 3 {
  366. sql_str = StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  367. } else { //默认左连接
  368. sql_str = StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  369. }
  370. }
  371. }
  372. if len(where) > 0 || len(where_or) > 0 {
  373. sql_str = StringJoin(sql_str, " where ")
  374. }
  375. if len(where) > 0 {
  376. sql_str = StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ")
  377. }
  378. if len(where_or) > 0 {
  379. if len(where) > 0 {
  380. sql_str = StringJoin(sql_str, " or ", strings.Join(where_or, " or "))
  381. } else {
  382. sql_str = StringJoin(sql_str, strings.Join(where_or, " or "))
  383. }
  384. }
  385. if orderby != "" {
  386. sql_str = StringJoin(sql_str, " order by ", orderby)
  387. }
  388. if page > 0 || page_size > 0 {
  389. if page < 1 {
  390. page = 1
  391. }
  392. if page_size < 1 {
  393. page_size = 10
  394. }
  395. from := strconv.Itoa((page - 1) * page_size)
  396. offset := strconv.Itoa(page_size)
  397. if from != "" && offset != "" {
  398. sql_str = StringJoin(sql_str, " limit ", from, " , ", offset)
  399. }
  400. }
  401. if debug {
  402. log.Println("query sql:", sql_str, valueList)
  403. }
  404. condition_len := 0 //所有条件数
  405. for _, ch2 := range sql_str {
  406. if string(ch2) == "?" {
  407. condition_len++
  408. }
  409. }
  410. if condition_len != len(valueList) {
  411. return 0, list, errors.New("参数错误,条件值错误")
  412. }
  413. var rows *sql.Rows
  414. var err error
  415. var queryNum int = 0
  416. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  417. rows, err = dbconn.Query(sql_str, valueList...)
  418. if err == nil {
  419. break
  420. } else {
  421. log.Println(err)
  422. time.Sleep(time.Millisecond * 500)
  423. }
  424. queryNum++
  425. }
  426. if err != nil {
  427. rows.Close()
  428. return 0, list, err
  429. }
  430. columns, _ := rows.Columns()
  431. scanArgs := make([]interface{}, len(columns))
  432. values := make([]interface{}, len(columns))
  433. for i := range values {
  434. scanArgs[i] = &values[i]
  435. }
  436. var index string
  437. var rowerr error
  438. var info map[string]string
  439. for rows.Next() {
  440. rowerr = rows.Scan(scanArgs...)
  441. info = make(map[string]string)
  442. if rowerr == nil {
  443. for i, col := range values {
  444. if col != nil {
  445. index = StrFirstToUpper(columns[i])
  446. info[index] = ToString(col)
  447. }
  448. }
  449. count++
  450. } else {
  451. log.Println("ERROR", rowerr)
  452. }
  453. if len(info) > 0 {
  454. list = append(list, info)
  455. }
  456. }
  457. rows.Close()
  458. return count, list, nil
  459. }
  460. func GetInfo(dbName, table string, title string, where map[string]string) (map[string]string, error) {
  461. count, info, gzErr := GetData(dbName, table, title, where, nil)
  462. if gzErr != nil {
  463. return info, gzErr
  464. } else {
  465. if count < 1 {
  466. return info, errors.New("No data")
  467. }
  468. return info, nil
  469. }
  470. }
  471. /**
  472. * 查询列表
  473. * 2018/04/19
  474. */
  475. func GetList(dbName, table string, title string, where map[string]string, limit map[string]string) ([]map[string]string, error) {
  476. var list []map[string]string
  477. if dbName == "" && table == "" {
  478. return list, errors.New("没有数据表")
  479. }
  480. dbName = getTableName(dbName, table)
  481. var rows *sql.Rows
  482. var err error
  483. var queryNum int = 0
  484. var limitStr string = ""
  485. if len(limit) > 0 {
  486. var offset string = "0" //偏移量,个数
  487. var from string = "" //开始
  488. if _, ok := limit["order"]; ok {
  489. limitStr += " order by " + limit["order"]
  490. }
  491. if _, ok := limit["offset"]; ok {
  492. offset = limit["offset"]
  493. }
  494. if _, ok := limit["from"]; ok {
  495. from = limit["from"]
  496. }
  497. if offset != "0" && from != "" {
  498. limitStr += " limit " + from + "," + offset
  499. }
  500. }
  501. if len(where) > 0 {
  502. valueList := make([]interface{}, len(where))
  503. whereStr := make([]string, len(where))
  504. i := 0
  505. var keys []string
  506. for key, value := range where {
  507. key = strings.Trim(key, " ")
  508. value = strings.Trim(value, " ")
  509. if value == "" || key == "" {
  510. continue
  511. // return list, errors.New("Query condition is empty")
  512. }
  513. if strings.Contains(key, " ") {
  514. //key中包含空格,判断是否不等于条件
  515. keys = strings.Split(key, " ")
  516. if IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  517. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  518. }
  519. } else {
  520. whereStr[i] = key + " = ?"
  521. valueList[i] = value
  522. }
  523. i++
  524. }
  525. if len(whereStr) == 0 || len(valueList) == 0 {
  526. return list, errors.New("Query condition is empty")
  527. }
  528. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  529. rows, err = DB.Query("select "+title+" from "+dbName+" where "+strings.Join(whereStr, " and ")+" "+limitStr, valueList...)
  530. if err == nil {
  531. break
  532. } else {
  533. time.Sleep(time.Millisecond * 500)
  534. }
  535. queryNum++
  536. }
  537. } else {
  538. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  539. rows, err = DB.Query("select " + title + " from " + dbName + " " + limitStr)
  540. if err == nil {
  541. break
  542. } else {
  543. time.Sleep(time.Millisecond * 500)
  544. }
  545. queryNum++
  546. }
  547. }
  548. if err != nil {
  549. return list, err
  550. }
  551. defer rows.Close()
  552. columns, _ := rows.Columns()
  553. scanArgs := make([]interface{}, len(columns))
  554. values := make([]interface{}, len(columns))
  555. for i := range values {
  556. scanArgs[i] = &values[i]
  557. }
  558. var record map[string]string
  559. var index string
  560. for rows.Next() {
  561. //将行数据保存到record字典
  562. err = rows.Scan(scanArgs...)
  563. record = make(map[string]string)
  564. for i, col := range values {
  565. if col != nil {
  566. index = StrFirstToUpper(columns[i])
  567. record[index] = ToString(col)
  568. }
  569. }
  570. list = append(list, record)
  571. }
  572. return list, nil
  573. }
  574. /**
  575. * 查询总数
  576. * 2018/04/19
  577. */
  578. func GetTotal(dbName, table string, args ...string) (total int) {
  579. if dbName == "" && table == "" {
  580. return
  581. }
  582. dbName = getTableName(dbName, table)
  583. var title string = "*"
  584. if len(args) > 0 {
  585. title = args[0]
  586. }
  587. var rows *sql.Rows
  588. var err error
  589. var queryNum int = 0
  590. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  591. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  592. if err == nil {
  593. break
  594. } else {
  595. time.Sleep(time.Millisecond * 500)
  596. }
  597. queryNum++
  598. }
  599. if err != nil {
  600. log.Println("ERROR|get", dbName, "total error", err)
  601. return
  602. }
  603. defer rows.Close()
  604. var count int
  605. for rows.Next() {
  606. //将行数据保存到record字典
  607. err = rows.Scan(&count)
  608. if err != nil {
  609. log.Println("ERROR|get", dbName, "total error", err)
  610. } else {
  611. total = count
  612. }
  613. }
  614. return
  615. }
  616. /**
  617. * 查询总数
  618. * 2020/06/04
  619. */
  620. func GetCount(dbName, table string, where map[string]string, args ...string) (total int) {
  621. if dbName == "" && table == "" {
  622. return
  623. }
  624. dbName = getTableName(dbName, table)
  625. var title string = "*"
  626. if len(title) > 0 {
  627. title = args[0]
  628. }
  629. var rows *sql.Rows
  630. var err error
  631. var queryNum int = 0
  632. if len(where) > 0 {
  633. valueList := make([]interface{}, len(where))
  634. whereStr := make([]string, len(where))
  635. i := 0
  636. var keys []string
  637. for key, value := range where {
  638. key = strings.Trim(key, " ")
  639. value = strings.Trim(value, " ")
  640. if value == "" || key == "" {
  641. continue
  642. // return list, errors.New("Query condition is empty")
  643. }
  644. if strings.Contains(key, " ") {
  645. //key中包含空格,判断是否不等于条件
  646. keys = strings.Split(key, " ")
  647. if IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  648. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  649. }
  650. } else {
  651. whereStr[i] = key + " = ?"
  652. valueList[i] = value
  653. }
  654. i++
  655. }
  656. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  657. rows, err = DB.Query("select count("+title+") as count from "+dbName+" where "+strings.Join(whereStr, " and ")+" limit 1", valueList...)
  658. if err == nil {
  659. break
  660. } else {
  661. time.Sleep(time.Millisecond * 500)
  662. }
  663. queryNum++
  664. }
  665. } else {
  666. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  667. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  668. if err == nil {
  669. break
  670. } else {
  671. time.Sleep(time.Millisecond * 500)
  672. }
  673. queryNum++
  674. }
  675. }
  676. if err != nil {
  677. log.Println("ERROR|get", dbName, "count error", err)
  678. return
  679. }
  680. defer rows.Close()
  681. var count int
  682. for rows.Next() {
  683. //将行数据保存到record字典
  684. err = rows.Scan(&count)
  685. if err != nil {
  686. log.Println("ERROR|get", dbName, "count error", err)
  687. } else {
  688. total = count
  689. }
  690. }
  691. return
  692. }
  693. func DoQuery(args ...interface{}) ([]map[string]string, error) {
  694. var list []map[string]string
  695. if len(args) < 1 {
  696. return list, errors.New("Query condition is empty")
  697. }
  698. queryStr := ToString(args[0])
  699. if queryStr == "" {
  700. return list, errors.New("Query condition is empty")
  701. }
  702. var rows *sql.Rows
  703. var err error
  704. var queryNum int = 0
  705. for queryNum < 3 { //如发生错误,继续查询5次,防止数据库连接断开问题
  706. if len(args) > 1 {
  707. rows, err = DB.Query(queryStr, args[1:]...) //strings.Join(args[1:], ",")
  708. if err != nil {
  709. log.Println("ERROR|DoQuery error:", err)
  710. }
  711. } else {
  712. rows, err = DB.Query(queryStr)
  713. if err != nil {
  714. log.Println("ERROR|DoQuery error:", err)
  715. }
  716. }
  717. if err == nil {
  718. break
  719. } else {
  720. time.Sleep(time.Millisecond * 500)
  721. }
  722. queryNum++
  723. }
  724. if err != nil {
  725. return list, err
  726. }
  727. defer rows.Close()
  728. columns, _ := rows.Columns()
  729. scanArgs := make([]interface{}, len(columns))
  730. values := make([]interface{}, len(columns))
  731. for i := range values {
  732. scanArgs[i] = &values[i]
  733. }
  734. var record map[string]string
  735. var index string
  736. for rows.Next() {
  737. //将行数据保存到record字典
  738. err = rows.Scan(scanArgs...)
  739. record = make(map[string]string)
  740. for i, col := range values {
  741. if col != nil {
  742. index = StrFirstToUpper(columns[i])
  743. record[index] = ToString(col)
  744. }
  745. }
  746. list = append(list, record)
  747. }
  748. return list, nil
  749. }