Lua不会覆盖零值

  • 本文关键字:覆盖 Lua lua
  • 更新时间 :
  • 英文 :


我有一个sql语句,它可以返回结果,也可以不返回结果。如果它没有返回结果,我需要将nil值更改为"0";没有";。我似乎不知道该怎么做。我已经把我的代码放在pcall中了,仍然不会被覆盖。我不断地得到";尝试索引零值";在if语句行中。我正在Debian 8上运行lua 5.2.3。我错过了什么?

--if ( SqlConnect(number).status == nil or SqlConnect(number).status == '') then
if pcall( SqlConnect(number).status ) then
result = "none"
else
result = SqlConnect(number).status
end

将pcall((与assert((组合为。。。

if pcall(assert,SqlConnect(number).status) then return true else return false end

然后在正确或错误部分做你必须做的事情。假设您需要该值,然后在true部分中执行pcall((以获得该值,并在false部分中执行回退情况。

如果pcall返回成功,并且给出了一个适当的值,那么它就使用它。否则,它将替换为您的"none"结果。

local success, result = pcall( SqlConnect(number).status )
if not success or result == '' or type( result ) == nil then
result = 'none'
end

编辑——同样的事情,只要点击它,反转它:

if not success or type( result ) == nil or result == '' then

编辑:

pcall((可能只希望函数作为arg,而不是附加的.status
我不确定,但如果非要我猜的话,那就是它失败的原因
https://riptutorial.com/lua/example/16000/using-pcall


以下是如何将其写成xpcall:

function try()
attempt = SqlConnect( number ) .status or 'none'  --  if nil, replace with 'none'
if attempt == '' then attempt = 'none' end  --  replace blank strings with 'none'
return attempt
end
function except()  --  if call to `SqlConnect( number )` completely fails
return 'none'
end
success, result = xpcall( try, except )

https://www.tutorialspoint.com/lua/lua_error_handling.htm

最新更新