将数据保存到文件中的Golang SQL查询。

huangapple go评论127阅读模式
英文:

Saving data to a file golang sql query

问题

帮助我,如何将接收到的数据写入文件?我需要将section_id和modified_by的所有数据写入文件。

  1. rows, err := db.Query("select section_id, modified_by from enrollment where rownum < 5")
  2. if err != nil {
  3. fmt.Println("Error running query")
  4. fmt.Println(err)
  5. return
  6. }
  7. defer rows.Close()
  8. var section_id string
  9. var modified_by string
  10. for cont := true; cont; cont = rows.NextResultSet() {
  11. for rows.Next() {
  12. err := rows.Scan(&section_id, &modified_by)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. fmt.Println(section_id, modified_by)
  17. // 在这里将数据写入文件
  18. }
  19. }

谢谢您的帮助!

英文:

Help me, pls. How can I write the received data on file? I need write all-data from section_id, modified_by.

  1. rows, err := db.Query(&quot;select section_id, modified_by from enrollment where rownum &lt; 5&quot;)
  2. if err != nil {
  3. fmt.Println(&quot;Error running query&quot;)
  4. fmt.Println(err)
  5. return
  6. }
  7. defer rows.Close()
  8. var section_id string
  9. var modified_by string
  10. for cont := true; cont; cont = rows.NextResultSet() {
  11. for rows.Next() {
  12. err := rows.Scan(&amp;section_id, &amp;modified_by)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. fmt.Println(section_id, modified_by)
  17. }
  18. }
  19. }

Thank you for any help!

答案1

得分: 0

用以下代码替换循环部分:

  1. f, err := os.Create("data.txt")
  2. if err != nil {
  3. log.Fatalf("无法打开文件:%v", err)
  4. }
  5. defer f.Close()
  6. for cont := true; cont; cont = rows.NextResultSet() {
  7. for rows.Next() {
  8. err := rows.Scan(&section_id, &modified_by)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. fmt.Println(section_id, modified_by)
  13. n, err := f.WriteString(fmt.Sprintf("%s 修改了 %d", modified_by, section_id))
  14. if err != nil {
  15. log.Fatalf("无法写入文件:%v", err)
  16. }
  17. log.Printf("写入了 %d 字节\n", n)
  18. }
  19. }

首先打开一个文件,并检查是否有错误,然后在循环中再次将每一行写入文件,并在其中进行错误检查。更多示例请参见这里

英文:

Replace the loop with

  1. f, err := os.Create(&quot;data.txt&quot;)
  2. if err != nil {
  3. log.Fatalf(&quot;could not open file: %v&quot;, err)
  4. }
  5. defer f.Close()
  6. for cont := true; cont; cont = rows.NextResultSet() {
  7. for rows.Next() {
  8. err := rows.Scan(&amp;section_id, &amp;modified_by)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. fmt.Println(section_id, modified_by)
  13. n, err := f.WriteString(fmt.Sprintf(&quot;%s modified %d&quot;, modified_by, section_id))
  14. if err != nil {
  15. log.Fatalf(&quot;could not write to file: %v&quot;, err)
  16. }
  17. log.Printf(&quot;Wrote %d bytes\n&quot;, n)
  18. }
  19. }

You first open a file, check for errors and then in the loop write every row to the file again with error checks in between. See here for more examples.

答案2

得分: 0

你可以使用json.MarshalIndent将数据保存为格式化的json文件。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. )
  7. func ToJsonFile(path string, v interface{}) {
  8. bytes, _ := json.MarshalIndent(v, "", " ")
  9. if err := ioutil.WriteFile(path, bytes, 0644); err != nil {
  10. fmt.Println(err)
  11. panic(err)
  12. }
  13. fmt.Println("已将数据保存为json文件,路径为:" + path)
  14. }
  15. type Enrollment struct {
  16. SectionId string `json:"section_id"`
  17. ModifiedBy string `json:"modified_by"`
  18. }
  19. func PersistData() error {
  20. // 在这里实现数据库操作...
  21. // 用于存放数据的数组
  22. var data []*Enrollment
  23. rows, err := db.Query("select section_id, modified_by from enrollment where rownum < 5")
  24. if err != nil {
  25. fmt.Println("运行查询时出错")
  26. fmt.Println(err)
  27. return err
  28. }
  29. defer rows.Close()
  30. for cont := true; cont; cont = rows.NextResultSet() {
  31. for rows.Next() {
  32. document := &Enrollment{}
  33. err := rows.Scan(&document.SectionId, &document.ModifiedBy)
  34. if err != nil {
  35. fmt.Println(err)
  36. return err
  37. }
  38. data = append(data, document)
  39. fmt.Println(document.SectionId, document.ModifiedBy)
  40. }
  41. }
  42. // 将数据持久化为json文件
  43. ToJsonFile("DATA.json", data)
  44. return nil
  45. }

以上是要翻译的内容。

英文:

You can save your data as a beautified json file using json.MarshalIndent.

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. )
  7. func ToJsonFile(path string, v interface{}) {
  8. bytes, _ := json.MarshalIndent(v, &quot;&quot;, &quot; &quot;)
  9. if err := ioutil.WriteFile(path, bytes, 0644); err != nil {
  10. fmt.Println(err)
  11. panic(err)
  12. }
  13. fmt.Println(&quot;Saved the data as json file at &quot; + path)
  14. }
  15. type Enrollment struct {
  16. SectionId string `json:&quot;section_id&quot;`
  17. ModifiedBy string `json:&quot;modified_by&quot;`
  18. }
  19. func PersistData() error {
  20. // implement db here ...
  21. // array to put data together
  22. var data []*Enrollment
  23. rows, err := db.Query(&quot;select section_id, modified_by from enrollment where rownum &lt; 5&quot;)
  24. if err != nil {
  25. fmt.Println(&quot;Error running query&quot;)
  26. fmt.Println(err)
  27. return err
  28. }
  29. defer rows.Close()
  30. for cont := true; cont; cont = rows.NextResultSet() {
  31. for rows.Next() {
  32. document := &amp;Enrollment{}
  33. err := rows.Scan(&amp;document.SectionId, &amp;document.ModifiedBy)
  34. if err != nil {
  35. fmt.Println(err)
  36. return err
  37. }
  38. data = append(data, document)
  39. fmt.Println(document.SectionId, document.ModifiedBy)
  40. }
  41. }
  42. // persist data to a json file
  43. ToJsonFile(&quot;DATA.json&quot;, data)
  44. return nil
  45. }

huangapple
  • 本文由 发表于 2022年8月26日 21:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73502000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定