MySQLdb and dictcursor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • borris

    #1

    MySQLdb and dictcursor

    doesn anyone know a good reference, tute or examples of
    MySQLdb's dictCursor
    I want to pass dictionaries into the sql exec statements.
    I could only succeed with text as values


    this is how I got the cursor object to call cursor.execute( "query")
    connection = MySQLdb.connect ( host = "localhost" , user =
    self.config.use rName, passwd = self.config.pas sword, cursorclass =
    MySQLdb.cursors .DictCursor)

    thanks
  • Christoph Haas

    #2
    Re: MySQLdb and dictcursor

    On Monday 24 July 2006 14:06, borris wrote:
    doesn anyone know a good reference, tute or examples of
    MySQLdb's dictCursor
    I want to pass dictionaries into the sql exec statements.
    I could only succeed with text as values
    A german linux magazin has an article about passing a *list* of items at


    For those who don't understand german here comes an example on how to pass
    lists:

    cursor.execute( """INSERT INTO Adressen (Name, Strasse, PLZ, Ort)
    VALUES (%s, %s, %s, %s)""",
    [ ('Dr. Hans Mustermann', 'Musterstraße 13', 50823, 'Köln'),
    ('Peter Lustig', 'Im Bauwagen 2', 50827, 'Porz'),
    ('Edmund Stoiber', 'Spendensumpf 1', 47011, 'Bimbesdorf'),
    ('Onkel Hotte', 'Im Siff 42', 57072, 'Siegen'),
    ('Gerhard Schröder', 'Großmaulweg 2', 11901, 'Worthülsen') ]
    )

    However the DictCursor is helpful for *returning* dictionaries of values
    instead of just tuples. I always use DictCursor because it makes it
    clearer which rows I want to access in the result. But DictCursor can't
    pass dictionaries in a cursor.execute AFAIKT.

    But with dict.iteritems that shouldn't be hard to send dictionary items to
    the SQL database in a loop either.

    Regards
    Christoph
    --
    ~
    ~
    ".signature " [Modified] 1 line --100%-- 1,48 All

    Comment

    • blank

      #3
      Re: MySQLdb and dictcursor

      Dennis Lee Bieber <wlfraed@ix.net com.comwrote in
      news:duu9c2pppi qv45pg9a8moh027 33njcb7ch@4ax.c om:
      dictCursor
      RETURNS the results as a dictionary; it doesn't affect how
      parameters are passed in.
      thats how I was using it

      >
      Normally results are a (list or tuple) where you have to know the
      order of the fields specified in the query:
      >
      cr.execute("sel ect a, c, b from table")
      dt = cr.fetchone()
      >
      dt is a (list/tuple) with (a_value, c_value, b_value)
      >
      With a dictCursor you get
      >
      dcr.execute("se lect a, c, b from table")
      ddt = dcr.fetchone()
      >
      ddt is a dictionary of {"a" : a_value, "b" : b_value, "c" : c_value}
      >
      >
      MySQLdb nominally uses just the %s placeholder style, but I think
      it
      will also function with %(name)s format...
      >
      cr.execute(
      "insert into table (c, a, b) values (%(c)s, %(a)s, %(b)s",
      ddt)
      sounds a lot simpler, ill give it a go later. thanks


      Comment

      Working...