MySQL Select, from tuple to list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sithlordkyle
    New Member
    • Dec 2011
    • 3

    MySQL Select, from tuple to list

    Hopefully I can explain this correctly. I am trying to take a MySQL select statement, get the output into a list, then upload that list back to the MySQL server.

    Code:
    cursor.execute("SELECT stuff FROM place WHERE thing = 3 ORDER BY track_id ASC")
    This spits out a tuple of integers ...

    ((1L,), (2L,), (3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,), (11L,))

    ... with an 'L' following each one.

    But I want to update the database with this set as:

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

    At first I thought I had it, but each converted row of tuples was the same thing even though the actually data pulled had changed. Anyone have any ideas how to do this?
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    It sounds like you want to convert something from a tuple containing a long integer into an integer?

    Code:
    for t in ((1L,), (2L,), (3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,), (11L,)):
        print int(t[0])

    Comment

    • sithlordkyle
      New Member
      • Dec 2011
      • 3

      #3
      Glenton, thanks for the response. I actually got it to give me the list without the L in it. But that's half the thing. I need to dump those numbers back into a list and insert that list into a MySQL database.

      I think that PHP will provide a better solution than Python at this point.

      Comment

      • sithlordkyle
        New Member
        • Dec 2011
        • 3

        #4
        Actually, I figured it out myself...

        Code:
        for n in range(0,75):
          cursor.execute("SELECT row FROM table WHERE column = %s ORDER BY column ASC", (n))
          result = cursor.fetchall()
          List = list()
          for t in result:
            List.append(int(t[0]))
          List = ",".join(map(str,List))
          ListUpdate = cursor.execute("UPDATE table SET column = %s WHERE column = %s", (str(List),n))
        This will take the SELECT statement from MySQL as a tuple, format it into a list without the L or parenthesis, then UPDATE the MySQL with the list.

        Comment

        Working...