Sql选择 – 返回的总行数

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

Sql Select - Total Rows Returned

问题

使用database/sql包和Postgres和Mysql的驱动程序,我想要实现以下功能。我想要能够选择一行并知道是否有零行、一行或多行。QueryRow函数无法实现这一点,因为据我所知,它会返回一行,而不管是否有多行。对于我的情况,多行可能是一个错误,我想要知道这一点。我想要创建一个通用的函数来实现这个功能。<br><br>我尝试创建一个使用Query函数的函数,但我不知道如何在有多行的情况下返回第一行。我想要返回有多行的事实,但我也想要返回第一行。为了确定是否有多行,我必须执行Next操作,这会覆盖第一行。显然,我可以在不创建通用函数的情况下实现这一点,但我想要一个函数来做这个,因为我需要在多个地方这样做。<br><br>请问有人可以向我解释如何实现这个功能吗?即,当成功执行Next操作或Next返回空时,如何从函数中返回第一行。

英文:

Using the database/sql package and drivers for Postgres and Mysql I want to achieve the following. I want to be able to Select one row and know that there is either zero rows, one row, or more than one row. the QueryRow function does not achieve that, because as far as I can ascertain, it will return one row without error regardless of if there is more than one row. For my situation, more than one row may be an error, and I want to know about it. I want to create a general function to do this.<br><br>I looked at creating a function that uses the Query function, but I do not know how to return the first row if there is more than one row. I want to return the fact that there is more than one row, but I also want to return the first row. To determine that there is more than one row, I have to do a Next, and that overwrites the first row. Obviously I can achieve this without creating a general function, but I want a function to do it because I need to do this in a number of places<br><br>Could someone please explain to me how to achieve this. IE. To return the first row from a function when a successful Next has been done or the Next returned nothing.

答案1

得分: 1

我使用了database/sqlMySQLDriver来实现这个功能。你可以在https://github.com/go-sql-driver/下载MySQLDriver

我自己编写了execQuery函数来从数据库中获取一行或多行数据。它基于MySQL,但我认为也可以用类似的方式用于Postgres。

假设你有一个名为test的数据库表,其中有idnameage这几列。

代码:

var db *sql.DB // 它应该通过"sql.Open()"进行初始化

func execQuery(SQL string, args ...interface{}) (rows *sql.Rows, is_succeed bool) {
  rows, err := db.Query(SQL, args...)
  var ret bool
  if err == nil && rows != nil { // 如果数据库查询出错,rows将为nil,返回false
    ret = true
  } else {
    ret = false
  }
  
  return rows, ret
}

用法:

var name, age string
rows, is_succeed = execQuery("SELECT `name`, `age` FROM `test` WHERE `id` = ?", "123")
if !is_succeed {
  // 出错
  return 
}
for rows.Next() { // 如果结果集为空,这个循环不会执行
  err := rows.Scan(&name, &age)
  // 检查是否有错误并做一些处理
}

如果你想知道返回了多少行数据,只需在for循环中添加一个计数器,使用SQL也可以实现这个功能。

sql.Rows类似于一个列表结构,rows *sql.Rows指向返回行的第一行。使用rows.Next()来遍历每一行。我想这就是你所问的。

如果你真的经常需要知道行数,可以使用像memcacheDBRedis这样的缓存机制,或者只需自己实现一个简单的计数器来解决这个问题。

英文:

I'm using both database/sql & MySQLDriver to achieve this. You can download MySQLDriver at https://github.com/go-sql-driver/ .

I wrote execQuery function myself to get one or more rows from database. It's based on MySQL but I think it can also used to Postgres with similar implement.

Assume you have a DB table named test, and have rows named id, name, age.

Code:

var db *sql.DB // it should be initialized by &quot;sql.Open()&quot;

func execQuery(SQL string, args ...interface{}) (rows *sql.Rows, is_succeed bool) {
  rows, err := db.Query(SQL, args...)
  var ret bool
  if err == nil &amp;&amp; rows != nil { // if DB query error rows will be nil, it will return false
    ret = true
  } else {
    ret = false
  }
  
  return rows, ret
}

Usage:

var name, age string
rows, is_succeed = execQuery(&quot;SELECT `name`, `age` FROM `test` WHERE `id` = ?&quot;, &quot;123&quot;)
if !is_succeed {
  // error
  return 
}
for rows.Next() { // if have zero result rows, this for route won&#39;t execute
  err := rows.Scan(&amp;name, &amp;age)
  // check if has error &amp; do something
}

If you want to know how much rows returned, just add a counter in for route, use SQL can also achieve this.

sql.Rows likes a list structure, rows *sql.Rows points on first row of returned rows. Use rows.Next() to traverse every rows. I think that's what you've asked.

If you really want to know rows count very often, using a cache mechanic like memcacheDB or Redis or just implement a simple counter yourself can help you solve the problem.

huangapple
  • 本文由 发表于 2013年4月23日 21:29:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/16170666.html
匿名

发表评论

匿名网友

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

确定