On 12/19/2010 3:48 PM, Maksymus007 wrote: > you get array of arrays. Technically, in strict Python terms what you get is a list of tuples.
Each element of the list is a tuple where each column from the query provides an element of each tuple. > First array contains rows. Every row is just an array of fields, order > is the same as in your query. > > On Sun, Dec 19, 2010 at 9:45 PM, Andy <selforgani...@gmail.com > <mailto:selforgani...@gmail.com>> wrote: > > I need to execute some SQL queries involving joins and it seems like > custom SQL is the way to go. A couple of questions: > > 1) In the doc (http://docs.djangoproject.com/en/1.2/topics/db/sql/ > #executing-custom-sql-directly > <http://docs.djangoproject.com/en/1.2/topics/db/sql/ > #executing-custom-sql-directly>) there's an example: > cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz]) > What is the "self.baz" argument? What does it do? > That is just to show how to put data into your queries to parameterize them. The first argument to .execute() is a query string with parameter marks inside it (the parameter mark can be something other than "%s" but I believe that works for both PostgreSQL and MySQL. The second argument should be a sequence (I use a tuple, as some database drivers insisted on that in the past) of data elements that will be used to replace the parameter markers. > 2) After I execute a SELECT query, how do I access the different > fields and different rows of the results? > See above. After you have called the cursor's .execute() method you then have to call a method the fetch the data. Most people will use the .fetchall() method, but there is also .fetchone() (that returns a single tuple containing the data for the next row in the result) and .fetchmany(N), which returns N rows if that many are left, otherwise all remaining rows. regards Steve -- DjangoCon US 2011 Portland, OR: September 6-8 http://djangocon.us/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.