Lua不会覆盖空值
2020-12-7 22:25:10
收藏:0
阅读:160
评论:2
我有一个可能返回结果或不返回结果的sql语句。如果它没有返回结果,我需要将空值改为"none"。我似乎无法弄清楚如何做到这一点。我已经将我的代码放在了pcall中,但仍无法被覆盖。我在if语句行中始终收到"attempt to index a nil value"的错误。我在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
点赞
用户3342050
如果 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() 可能只想要 那个函数 作为参数,而不是附加的 .status。
我不确定,但如果我必须猜测,那就是它失败的原因。
https://riptutorial.com/lua/example/16000/using-pcall
以下是转换成 xpcall 的方法:
function try()
attempt = SqlConnect( number ) .status or 'none' -- 如果为 nil,则替换为 'none'
if attempt == '' then attempt = 'none' end -- 将空字符串替换为 'none'
return attempt
end
function except() -- 如果对 `SqlConnect( number )` 的调用完全失败
return 'none'
end
success, result = xpcall( try, except )
2020-12-07 23:37:19
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

将pcall()与assert()结合起来,就像这样...
如果 pcall(assert,SqlConnect(number).status) 成功,则返回 true,否则返回 false。...然后在 true 或 false 部分执行必要的操作。 比如说,如果你需要这个值,则在 true 部分进行 pcall() 以获取该值,并在 false 部分执行回滚情况。