2

I'm using Python 2.6.4 and its module sqlite3 for a small database project and I have the following problem: I'm trying to use a user-defined function, I mean, a function that you define in Python to use after inside your queries. The function is a wrapper for other function I have in another module. The problem is that when doing the query, I always get an AttributeError exception with the message: 'builtin_function_or_method' object has no attribute 'execute' and I have no clue why. The code is the following. Could you point me what I'm doing wrong?

Thanks in advance.

Wrapper function:

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

Function that actually does the query. Note the use of "create_function" method of the Connection object.

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

And finally, the query. It is inside the module called "specificConf". So it is used correctly in the function above, the problem is not there.

SQL_SELECT_ARTIST_SIMILARNAME = u'''
SELECT  CODARTIST, NAME, NUMCD, ARESIMILAR(?, NAME) AS SIMILAR
FROM    ARTIST
WHERE   SIMILAR = 1  
'''

1 Answer 1

2
cur = con.cursor    # sets `cur` to a method

should be

cur = con.cursor()  # calls the method and sets `cur` to the return value

This is why you were getting the error saying cur has no execute attribute:

AttributeError exception with the message: 'builtin_function_or_method' object has no attribute 'execute'

Sign up to request clarification or add additional context in comments.

2 Comments

That's right, men. I still can't manage to make it run. But that was a good start. I can't believe I have spent such a big amount of time on that error. Thankyou. You know, sometimes 4 eyes see more than 2.
Other thing I had to do, just for others reference, is to pass name as a tuple. Originally it is a string, but a tuple is needed to do parametrisation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.