如何解决尝试在使用python的sqlite3中使用用户定义的函数时发生异常
|| 我正在将Python 2.6.4及其模块sqlite3用于一个小型数据库项目,但我遇到以下问题:我正在尝试使用用户定义的函数,我的意思是,您在Python中定义的要使用的函数在您查询之后。该功能是我在另一个模块中拥有的其他功能的包装。问题是在执行查询时,我总是收到一条AttributeError异常消息:\'builtin_function_or_method \'对象没有属性'\ execute \',我也不知道为什么。代码如下。您能指出我做错了什么吗? 提前致谢。 包装功能:def SQLAreSimilar(userSelectedName,artistName):
\'\'\'
Wrapper to the areSimilar function of strUtils to use it directly inside the
queries. Not to be called directly.
\'\'\'
if strUtils.areSimilar(userSelectedName,artistName):
return 1
else:
return 0
实际执行查询的功能。注意使用Connection对象的\“ create_function \”方法。
def getArtistsBySimilarName(name):
\'\'\'
Returns all artists with a similar name,according to the Levenshtein
distance. The DB is supposed to be initialised. Returns a dictionary of
dictionaries with the data of all similar artists indexed by its CODARTIST,the PK. The dictionary is empty if no row is returned. None is returned on
error.
\'\'\'
try:
con = sqlite3.connect(genericConf.SQL_DBNAME)
con.row_factory = sqlite3.Row
con.create_function(\"ARESIMILAR\",2,SQLAreSimilar)
cur = con.cursor
rows = cur.execute(specificConf.SQL_SELECT_ARTIST_SIMILARNAME,name)
retDict = {}
for row in rows:
d = {}
d[\'NUMCD\'] = row[\'NUMCD\']
d[\'NAME\'] = row[\'NAME\']
retDict[row[\'CODARTIST\']] = d
return retDict
except:
return None
最后是查询。它在名为“ specificConf”的模块内部。因此在上面的函数中正确使用了它,问题就不存在了。
SQL_SELECT_ARTIST_SIMILARNAME = u\'\'\'
SELECT CODARTIST,NAME,NUMCD,ARESIMILAR(?,NAME) AS SIMILAR
FROM ARTIST
WHERE SIMILAR = 1
\'\'\'
解决方法
cur = con.cursor # sets `cur` to a method
应该
cur = con.cursor() # calls the method and sets `cur` to the return value
这就是为什么您会收到错误消息,说cur
没有execute
属性的原因:
AttributeError异常,带有
讯息:\'builtin_function_or_method \'
对象没有属性“执行”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。