将值传递给python中函数内的函数

时间:2012-01-12 01:49:49

标签: python function function-parameter

我需要从函数中将值从一个函数传递给下一个函数。

例如(我的IRC bot编程为响应频道中的命令):

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.。

感谢您提供的任何帮助。

编辑 - 添加了我正在使用的代码。

2 个答案:

答案 0 :(得分:0)

你的意思是字符串“sql”“返回零” - 字符串不是函数或表达式所以它不会“返回”任何东西。字符串只是一个字面值。

你是说cursor.execute()返回零?什么是“光标”等于?你是否正确地将“光标”对象初始化为其他地方的东西?

如果“cursor”是一个全局对象,你可能需要声明它,如下所示:

全局光标

否则您将无法对其进行更改。

除此之外,我无法弄清楚你在做什么或出了什么问题。

答案 1 :(得分:0)

我不太确定这行代码:

sql = "SELECT `"+ cmd +"` FROM permissions WHERE nick = '"+ nick +"' and chan = '"+ chan +"'" # This returns 0

您似乎正在尝试确保查询字符串格式正确。但这不是必要的。你可以连接你的字符串查询,它会像这样工作:

sql = "SELECT " + cmd + " FROM permissions WHERE nick = " + nick + " and chan = " + chan

观察查询语句中的任何位置都没有单引号。