从 python 函数内向函数传递值
我需要将值从一个函数传递到函数内的下一个函数。
例如(我的 IRC 机器人被编程为响应频道中的命令):
def check_perms(nick,chan,cmd):
sql = "SELECT `"+ cmd +"` FROM permissions WHERE nick = '"+ nick +"' and chan = '"+ chan +"'" # This returns 0
#sql = "SELECT `restart` FROM permissions WHERE nick = 'Me' and chan = '#mychan'" # this works as intended
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
if (row[0] == 1): # Nick logged in and has permission
return 1
else: # nick does not have permissions
return 0
def com_restart(nick,chan):
perm = check_perms(nick,chan,"restart")
if (perm == 0): # nick did not have permission
irc.send("NOTICE "+ nick +" :Permission denied.\n")
elif (perm == 1): # nick has permission
irc.send("PRIVMSG "+ chan +" :I've been asked to restart myself by "+ nick +".\n")
nick = "Me" # This is determined by a bunch of regex splits and such
chan = "#mychan" # This is determined by regex splits as well
com_restart(nick,chan)
但是,当我尝试此操作时,似乎值没有传递到 SQL 查询,因此它返回 0。
感谢您提供的任何帮助。
编辑-添加了我现在正在使用的代码。
I need to pass values from one function to the next from within the function.
For example (my IRC bot programmed to respond to commands in the channel):
def check_perms(nick,chan,cmd):
sql = "SELECT `"+ cmd +"` FROM permissions WHERE nick = '"+ nick +"' and chan = '"+ chan +"'" # This returns 0
#sql = "SELECT `restart` FROM permissions WHERE nick = 'Me' and chan = '#mychan'" # this works as intended
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
if (row[0] == 1): # Nick logged in and has permission
return 1
else: # nick does not have permissions
return 0
def com_restart(nick,chan):
perm = check_perms(nick,chan,"restart")
if (perm == 0): # nick did not have permission
irc.send("NOTICE "+ nick +" :Permission denied.\n")
elif (perm == 1): # nick has permission
irc.send("PRIVMSG "+ chan +" :I've been asked to restart myself by "+ nick +".\n")
nick = "Me" # This is determined by a bunch of regex splits and such
chan = "#mychan" # This is determined by regex splits as well
com_restart(nick,chan)
When I try this, though, it seems the values do not get passed to the SQL query, so it returns 0.
Thanks for any help you can provide.
EDIT - Added code that I'm working with as it stands right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串“sql”“返回零”是什么意思——字符串不是函数或表达式,因此它不会“返回”任何内容。字符串只是一个文字值。
你是说cursor.execute()返回零吗? “光标”等于什么?您是否正确地将“光标”对象初始化为其他地方的对象?
如果“cursor”是一个全局对象,您可能必须这样声明它,如下所示:
全局光标,
否则您将无法对其进行更改。
除此之外,我无法弄清楚你在做什么或出了什么问题。
What do you mean the string "sql" "returns zero" -- a string isn't a function or expression so it doesn't "return" anything. A string is just a literal value.
Are you saying that cursor.execute() returns zero? What is "cursor" equal to? Do you correctly initialize the "cursor" object to something somewhere else?
if "cursor" is a global object you may have to declare it as such, like this:
global cursor
otherwise you won't be able to make changes to it.
Other than that I can't figure out what you're doing or what's wrong.
我不太确定这行代码:
看来您正在尝试确保查询字符串的格式正确。但这是没有必要的。您可以连接字符串查询,它会像这样正常工作:
观察查询语句中的任何位置都没有单引号。
I'm not so sure about this line of code:
It seems you're trying to make sure that the query strings are in proper format. But that's not necessary. You can concatenate your string query and it would work fine like this:
Observe that there are no single quotes anywhere in the query statement.