I was experimenting with database operations. For example,
我正在尝试数据库操作。例如,
begin
db_con = PG.connect :dbname => 'sureshkumar', :user => 'sureshkumar'
db_con.prepare 'get_result', "SELECT * from users;"
users_name = db_con.exec_prepared 'get_result'
if users_name != nil
puts users_name.values
else
puts "There is no users available in the table users"
end
rescue PG::Error => e
puts e.message
ensure
db_con.close if db_con
end
The table users
contains only two columns. They are,
表用户只包含两列。他们是,
user_id
user_name
If the table does not contain any row, the exec_prepared
statement does not return any row. In this case, why does the if condition not become false? How do I check if the exec_prepared
method doesn't return any row?
如果表不包含任何行,则exec_prepared语句不返回任何行。在这种情况下,为什么if条件不会变错?如何检查exec_prepared方法是否不返回任何行?
1 个解决方案
#1
2
users_name = db_con.exec_prepared 'get_result'
will never return nil
to users_name
.
users_name = db_con.exec_prepared'get_result'永远不会返回nil到users_name。
According to the documentation of PG::Connection#exec_prepared
it will return PG::Result
object. So, users_name
object can never be nil
. It will always be an instance of PG::Result
.
根据PG :: Connection#exec_prepared的文档,它将返回PG :: Result对象。因此,users_name对象永远不能为零。它将永远是PG :: Result的一个实例。
So, to check whether you have any rows returned from the query execution you need to check if this PG::Result
object has any values in it. In your case, users_name
variable contains the PG::Result
. So you will have to call ntuples
on it so see how many rows are returned from the query.
因此,要检查是否从查询执行返回任何行,您需要检查此PG :: Result对象中是否包含任何值。在您的情况下,users_name变量包含PG :: Result。因此,您必须在其上调用ntuples,以便查看从查询返回的行数。
so change your if clause to something like:
所以将你的if子句改为:
if users_name.ntuples > 0
puts "There are users"
else
puts "There is no users available in the table users"
end
See PG::Result
's documentation to know what other methods you can call on it to access the result.
请参阅PG :: Result的文档,了解可以调用的其他方法来访问结果。
#1
2
users_name = db_con.exec_prepared 'get_result'
will never return nil
to users_name
.
users_name = db_con.exec_prepared'get_result'永远不会返回nil到users_name。
According to the documentation of PG::Connection#exec_prepared
it will return PG::Result
object. So, users_name
object can never be nil
. It will always be an instance of PG::Result
.
根据PG :: Connection#exec_prepared的文档,它将返回PG :: Result对象。因此,users_name对象永远不能为零。它将永远是PG :: Result的一个实例。
So, to check whether you have any rows returned from the query execution you need to check if this PG::Result
object has any values in it. In your case, users_name
variable contains the PG::Result
. So you will have to call ntuples
on it so see how many rows are returned from the query.
因此,要检查是否从查询执行返回任何行,您需要检查此PG :: Result对象中是否包含任何值。在您的情况下,users_name变量包含PG :: Result。因此,您必须在其上调用ntuples,以便查看从查询返回的行数。
so change your if clause to something like:
所以将你的if子句改为:
if users_name.ntuples > 0
puts "There are users"
else
puts "There is no users available in the table users"
end
See PG::Result
's documentation to know what other methods you can call on it to access the result.
请参阅PG :: Result的文档,了解可以调用的其他方法来访问结果。