Menu

duplicate data in as_dict set

2009-06-26
2013-04-29
  • John Behling

    John Behling - 2009-06-26

    Hello:

    I would like to pull a dataset as a dictionary, however when I use the as_dict argument in pymssql I get a duplicate set of data, one with the column names and the other with the column numbers

    For example if the fields are firstname and lastname a one row query would look like {'0':'Smith','1':'John','firstname':'John','lastname':'Smith'}

    How do I eliminate the column number set? (or choose which kind of dictionary I'd like to have?

    Please advise,

    Thanks!

     
    • A

      A - 2009-06-27

      This way you can obtain only numeric indexes
      [tuple([row[r] for r in sorted(row.keys()) if type(r) == int]) for row in conn]

      this way the rest
      [tuple([row[r] for r in sorted(row.keys()) if type(r) != int]) for row in conn]

      adapt to your needs.

       
  • Vittorio Natale

    Vittorio Natale - 2010-03-25

    Hi, I'm a new pymssql user and I just downloaded and installed it today (version 1.0.2) and i have the same problem.   To solve it I have added some code to these functions in pymssql.py: fetchmany(), fetchone() and fetchmany().

    between lines 242 and 243 of fetchone() of original file, I added these 3 line:

    for i in row.keys():
             if type(i) == int:
                    del row
    _

    to delete the elements with interger key.

    Between lines 279 and 280 of fetchmany() of original file, I added these 3 line:

    for i in t.keys():
             if type(i) == int:
    del t

    and also between lines 305-306 I added these:

    for r in list:
          for i in r.keys():
    if type(i) == int:
    del r

    It work for me.. I hope I was hopefull..
    Bye_

     
  • Vittorio Natale

    Vittorio Natale - 2010-03-25

    Sorry about that … I don't know how to make the indenting come out correctly. All of the lines in that last post should have been formatted correctly and
    I'm sorry, not hopefull, but helfull :)

     
  • Vittorio Natale

    Vittorio Natale - 2010-03-25

    Sorry about that, I make an other mistake in all code I written before.
    About fecthone():

    for i in row.keys():
    if type(i) == int:
    del row_

    About fetchmany():

    for i in t.keys():
    if type(i) == int:
    del t

    About fetchall():

    for r in list:
    for i in r.keys():
    if type(i) == int:
    del r

    bye.._

     
  • Vittorio Natale

    Vittorio Natale - 2010-03-25

    Sorry about that, I make an other mistake in all code I written before.
    About fecthone():

    for x in row.keys():
    if type(x) == int:
    del row

    About fetchmany():

    for x in t.keys():
    if type(x) == int:
    del t

    About fetchall():

    for r in list:
    for x in r.keys():
    if type(x) == int:
    del r

    bye..

     

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.