On Feb 16, 10:29 am, MikeKJ <mike.jo...@paston.co.uk> wrote:
> I got these imported tables from an external source so I need to
> access the data using connection.

Not necessarily - if there's no compound primary key, you can build
django models from the tables using manage.py inspectdb (be sure to
add the managed=False and set the correct tablename in the generated
model's Meta).


> The problem I have is getting at the data in more than 1 table with
> one dict the pure sql call would be something like
>
> select * from yw_basics ywb left join yw_next ywn on ywb.id = ywn.id
> where ywn.field = 0

<ot>"select * " is usually a bad idea.</ot>

> def dictfetchall(cursor):
>     desc = cursor.description
>     return [
>         dict(zip([col[0] for col in desc], row))
>         for row in cursor.fetchall()
>     ]

You'd be better using lazy evaluation:

def dictfetchall(cursor):
    # avoid reexecuting constant code in a loop
    desc = col[0] for col in cursor.description
    for row in cursor:
        yield dict(zip(desc, row))


> def secondhand(request):
>     from django.db import connection, transaction

Please avoid imports in functions.

>     cursor = connection.cursor()
>     cursor.execute("SELECT * from yw_basics")
>     stuff = dictfetchall(cursor)
>
> I tried adding the sql call above into the cusor.execute and failed
> miserably so any clues would be gratefully received.

First clue : "failed miserably" is about the most useless possible
description of the problem, specially if you hope to get any help. At
the very least explain what happens (and if you get an exception post
the full traceback).

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to