Menu

ResultSet in Python ?

Help
Anonymous
2002-11-14
2012-09-19
  • Anonymous

    Anonymous - 2002-11-14

    Hi Group,
    i have a question about working with mysql.
    Has Python an Object like the ResultSet in Java? That allows me, after executing a statement, to get the Data by calling the Col-Name:
          String str = resSet.getString("col_name");

    Is there some similar in Python ?
    If yes, please give my a code-exsample

    thanks

     
    • Joakim Sernbrant

      I use a small class that wraps a row so you can access a column thruogh x.name.

      c.execute("select ...")
      r = DBRow(c)
      print r.foo, r.bar

      ----cut-here----

      class DBRow:
          def __init__(self, c, row=None):
              if row: self.__row = row
              else: self.__row = c.fetchone()
              self.__columns = map(lambda x: x[0], c.description)
              self.__colmap = {}
              for i in range(len(c.description)):
                  self.__colmap[c.description[i][0]] = i

          def __len__(self):
              if self.__row: return len(self.__row)
              else: return 0

          def __str__(self):
              return str(self.__row)

          def __getitem__(self, key):
              return self.__row[key]

          def __getattr__(self, name):
              return self.__row[self.__colmap[name]]

          def columns(self):
              return self.__columns

      ----cut-here----

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.