dongpengyu1363 2019-03-10 19:44
浏览 64
已采纳

如何避免在获取行数时使用下一个方法

In php, I can print rowcount where postid matches with the code below without passing the result in a while loop.

$status_query = "SELECT count(*) as postCount FROM postData WHERE postid=1";
$status_result = mysqli_query($con,$status_query);
$status_row = mysqli_fetch_array($status_result);
$postCount = $status_row['postCount'];
echo $postCount;

Now am re-writing the code to golang for the same row count. I leverage Stackoverflow solution found here. source link

With that Stackoverflow solution link above, the golang code below is working great as I can get the row count.

rows1, err := db.Query("SELECT COUNT(*) as postCount FROM postData WHERE postid=1")
if err != nil {
    log.Fatal(err)
}
defer rows1.Close()
var  postCount int

for rows1.Next() {   
    if err := rows1.Scan(& postCount); err != nil {
        log.Fatal(err)
    }
}

fmt.Printf("Number of rows are %s
",  postCount)

Here is what I want modify:

The above code passed the row count result within

for rows1. Next(){
// result here.
}

My question is:

Please how do I avoid this for rows.next() function and just get my result straight since am retrieving the row count based on postid. In php code above, I can get the result straight without passing it in a while loop.

In golang am thinking of something like the code below

rows1, err := db.Query("SELECT COUNT(*) as postCount FROM postData WHERE postid=1")
if err != nil {
    log.Fatal(err)
}
defer rows1.Close()
var status_row = rows1.Next() 

var postCount =rows1.Scan(& postCount)
fmt.Printf("Number of rows are %s
",  postCount)

Does anyone has a better way of getting this rowcount to display straight without passing the result within for rows1.Next() method

Here is the overall working code before I seek modification in the coding illustrations above.

package main

import "database/sql"
import _ "github.com/go-sql-driver/mysql"

import "net/http"
import "fmt"
import "log"

var db *sql.DB
var err error


func getRecordPage1(res http.ResponseWriter, req *http.Request) {

    if req.Method != "POST" {
        http.ServeFile(res, req, "getjsonRecord.html")
        return
    }

//The commented section below is the code I want to modify to avoid use of for rows.next function....
/*
rows1, err := db.Query("SELECT COUNT(*) as postCount FROM like_unlike WHERE postid=1")
if err != nil {
    log.Fatal(err)
}
defer rows1.Close()
var  postCount int

for rows1.Next() {   
    if err := rows1.Scan(& postCount); err != nil {
        log.Fatal(err)
    }
}

fmt.Printf("Number of rows are %s
",  postCount)

}
*/


func homePage(res http.ResponseWriter, req *http.Request) {
    http.ServeFile(res, req, "index.html")
}

func main() {
    db, err = sql.Open("mysql", "root:@/golang44")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err.Error())
    }

    http.HandleFunc("/getjsonRecord", getRecordPage1)
    http.HandleFunc("/", homePage)
        fmt.Println("Listening on 127.0.0.1:8088")
    http.ListenAndServe(":8088", nil)
}
  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2019-03-10 20:09
    关注

    Generally, if you know you are getting one row, use DB.QueryRow It allows you to chain the query and scan together, so your example would look like:

    var  postCount int
    err := db.QueryRow("SELECT COUNT(*) as postCount FROM postData WHERE postid=1").Scan(&postCount)
    if err != nil {
        log.Fatal(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符