尝试在使用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 \'   对象没有属性“执行”     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...