Database db.query中变量使用不当
我正在尝试使用webpy开发一个博客Database db.query中变量使用不当,database,web.py,Database,Web.py,我正在尝试使用webpy开发一个博客 def getThread(self,num): myvar = dict(numero=num) print myvar que = self.datab.select('contenidos',vars=myvar,what='contentTitle,content,update',where="category LIKE %%s%" %numero) r
def getThread(self,num):
myvar = dict(numero=num)
print myvar
que = self.datab.select('contenidos',vars=myvar,what='contentTitle,content,update',where="category LIKE %%s%" %numero)
return que
我用了一些你在这个网站上回答的技巧,但我只得到了一个答案
在/
未定义全局名称“numero”
Python C:\xampp\htdocs\webpy\functions.py,在getThread中,第42行
Web获取http://:8080/
我正在尝试选择一些分类文章。有一个具有类别名称和id的表。内容表中有一列包含一个字符串,该字符串的格式为“1,2,3,5”
然后我想我可以用LIKE语句和一些%something%的魔力来选择正确的条目。但我有这个问题
我从构建web的.py文件调用代码,import语句工作正常
getThread在此类中定义:
class categoria(object):
def __init__(self,datab,nombre):
self.nombre = nombre
self.datab = datab
self.n = str(self.getCat()) #making the integer to be a string
self.thread = self.getThread(self.n)
return self.thread
def getCat(self):
'''
returns the id of the categorie (integer)
'''
return self.datab.select('categorias',what='catId', where='catName = %r' %(self.nombre), limit=1)
请检查
db的正确语法。选择(),您不应使用“%”格式化查询,因为这会使代码容易受到sql注入的攻击。相反,将变量放在dict中,并在查询中使用$
引用它们
myvars = dict(category=1)
db.select('contenidos', what='contentTitle,content,`update`', where="category LIKE '%'+$category+'%'", vars=myvars)
将生成此查询:
SELECT contentTitle,content,`update` FROM contenidos WHERE category LIKE '%'+1+'%'
请注意,我回引了update
,因为它在SQL中是保留字。非常感谢,它成功了,它在dict中使用了var定义。问题是:没有使用vars=…
,而是使用单个cuotes“contentTitle,content,'update”更改了回引
该文档没有太多的精确性。我还有另一个类似的问题,我用db解决了它。查询:更直接地编写sql使事情变得更容易。我主要使用db.query
也因为它的灵活性,它还接受应该用于格式化查询的vars
dict。