英文:
Why are there only two query types in the Go Database library?
问题
根据我所了解,Go的database/sql
接口库只期望返回两种类型的结果 - 一行或者一组行。然而,至少还有一种结果类型 - 单列。
DB.column('SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL')
有没有办法处理这种情况 - 还是我只能获取一行然后从中访问COUNT(*)
?
英文:
From what I can tell, there are only two types of results the Go database/sql
interface library expects back - a row or an array of rows. However, there is at least one more type of result - a single column.
DB.column('SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL')
Is there any way to handle this - or do I just have to fetch a row and then access the COUNT(*)
from that?
答案1
得分: 2
是的,你可以获取一行单列数据,但是这很难吗?
var count int
row := db.QueryRow("SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL")
err := row.Scan(&count)
请注意,如果你觉得太冗长,可以将其压缩(可以删除row变量)。
我认为其他类似的系统在其他语言中(例如JDBC)也没有原生提供这种快捷方式。
我发现处理一个我可以记住和浏览的API比处理一个拥有我可能愿意使用的所有实用工具以删除我的代码中的一行更容易。
英文:
Yes you fetch a one column row but is that so hard ?
var count int
row := db.QueryRow("SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL")
err := row.Scan(&count)
Note that this may be compacted if you find it too verbose (you may remove the row variable).
I think that other similar systems in other languages (for example JDBC) don't offer natively this shortcut either.
I find easier to handle an API that I can memorize and browse rather than an API which has all the utilities I might be willing to use to remove one line in my code.
答案2
得分: 2
为了记录,SQL Server存储过程同时返回以下内容:
- 一个整数返回码
- 零个或多个消息(通常是警告或错误),包含文本和两个整数代码
- 零个或多个命名的、有类型的标量输出参数
- 零个或多个“行集”,每个行集都是一个有序的零个或多个行的列表。
在行集内,所有行都有相同数量(一个或多个)的命名、有类型的列。列名在行集内不必唯一。
SQL Server不识别任何特殊情况,比如只有一个行集和一个行,或者只有一个输出参数。
其他数据库系统略有不同。
英文:
For the record, a SQL Server stored procedure returns all of the following (at the same time):
- an integer return code
- zero or more messages (often warnings or errors) containing text and two integer codes
- zero or more named, typed scalar output parameters
- zero or more "rowsets", each of which is an ordered list of zero or more rows.
Within a rowset, all rows have the same number (one or more) of named, typed columns. The column names do not have to be distinct within a rowset.
SQL Server does not recognize any special cases, like a single rowset with a single row or a single column; or a single output parameter.
Other database systems are slightly different.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论