1

I am about to smash my head against the wall.

Consider the following piece of code:

def get_cardnumbers_from_timestamp(request, since_time=0):
    if request.method == 'GET':
        if request.user:
            if not request.user.is_authenticated():
                logger.debug('User is not authorized')
                return auth_required('Authentication required')

        logger.debug('User has been authorized')
        user = request.user
        logger.debug('since_time: {0}'.format(since_time))
        if int(since_time) != 0:
            utc_time = time.mktime(time.gmtime(int(since_time)))
        else:
            utc_time = 0

        logger.debug('since_time UTC: {0}'.format(utc_time))

        devices_data = dict()
        from django.db import connection

        cursor = connection.cursor()
        query = 'SELECT c.name,c.id,d.locale,d.card_number,d.dev_uniqid,d.id FROM device_channel dc JOIN channels c on dc.channel_id = c.id JOIN devices d on dc.device_id=d.id WHERE d.card_number IS NOT NULL AND updated >= FROM_UNIXTIME({time}) ORDER BY d.id'.format(time=int(utc_time))
        logger.debug(query)
        cursor.execute(query)
        answers = cursor.fetchall()
        cursor.close()
        logger.debug(answers)

Formed query, when fired against MySQL with since_time either explicitly set to 0 or not set at all returns about 250 rows, so does Django.

However, when I set the timestamp to a certain value, doesn't matter which one, same formed query (which I log and then copy-paste from the log) returns, say, 5 rows in MySQL console. Django gives me a result set of whopping 0.

I might be overlooking something very obvious, but I've already tried numerous approaches at this, to no avail.

Any ideas would be most welcome.

3
  • did you run the same query in mysql console ? Commented Jun 17, 2014 at 15:30
  • @dhana: That's exactly what my question states - MySQL gives me results, Django doesn't :) Commented Jun 17, 2014 at 15:39
  • Im having the same issue :(. my query is working in SQL gui but exact same query not working in django raw query Commented Apr 18, 2017 at 11:19

2 Answers 2

3

You should not pass parameters to django raw queries with string format but instead pass them to execute (also check the docs). For instance, in your case do this:

query = """
 SELECT c.name,c.id,d.locale,d.card_number,d.dev_uniqid,d.id 
 FROM device_channel dc JOIN channels c on dc.channel_id = c.id 
 JOIN devices d on dc.device_id=d.id 
 WHERE d.card_number IS NOT NULL AND updated >= FROM_UNIXTIME(%s) 
 ORDER BY d.id
"""
cursor.execute(query, [int(utc_time)])
Sign up to request clarification or add additional context in comments.

2 Comments

Tried that as well. Same effect. No results :(
Hmm, although.. I probably was a bit off the rails the time I tried. I just re-tried your approach now and it does work.. Thanks man!
0

By default, the Python DB API will return results without their field names, which means you end up with a list of values, rather than a dict. At a small performance cost, you can return results as a dict by using something like this:

def dictfetchall(cursor):
    "Returns all rows from a cursor as a dict"
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
    ]

Comments

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.