Thread: MySQL results

    #1
  1. No Profile Picture
    VA Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Virginia/San Diego (Soon)
    Posts
    48
    Rep Power
    12

    MySQL results


    When I run a "show tables" query on a database and do a for loop through the array all of my results contain parenthesis and single quotes. Am I doing something wrong? I used the cursor.fetchall() function and had to use strip to clean up the extra characters. I must be doing something wrong so if someone could explain why this occurred I would be greatful
  2. #2
  3. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Could you post the code in question, as well as an example of its use? It's a little difficult to diagnose the problem otherwise.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm guessing by "show table" that your running a query against every table at once because it souds like fetchall() is returning a tuple which contains other tuples..

    Anyway i'm with telex, post your code

    Mark
    Last edited by netytan; October 19th, 2003 at 06:16 PM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    VA Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Virginia/San Diego (Soon)
    Posts
    48
    Rep Power
    12
    import MySQLdb
    connection = MySQLdb.connect(user='user',passwd='pass', db='db')
    cursor = connection.cursor()
    query="SHOW TABLES"
    cursor.execute(query)
    RowNum=cursor.fetchall()
    for x in RowNum:
    print x


    shows up as
    -----------------------------
    ('companies',)
    ('competition',)
    ('links',)
    ('notes',)


    what is a tupple, I have never heard that expression, is it only in python?

    the "show tables" should only return the tables in the database in an array, so im not exactly sure why it would have any other information.

    Thanks
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    a tuple is a data type similar to a list/array except that it's imutable (you can't change the values directly like you can with lists) represented by parenthesis

    Anyway give this a go - all i've done with this is print x[0] - which prints out the first value of x instread of the whole thing:

    Code:
    import MySQLdb 
    connection = MySQLdb.connect(user='user',passwd='pass', db='db') 
    cursor = connection.cursor() 
    query="SHOW TABLES" 
    cursor.execute(query) 
    RowNum=cursor.fetchall() 
    for x in RowNum: 
        print x[0]
    Have fun,
    Mark.
    Last edited by netytan; October 19th, 2003 at 06:16 PM.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    VA Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Virginia/San Diego (Soon)
    Posts
    48
    Rep Power
    12
    worked perfectly, thanks
  12. #7
  13. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Btw, it's spelt "tuple"
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Damn my bad spelling i didn't think it looked right, its just turning into one of those days.. well, nights

    Think i need to sleep soon lol

    Mark
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo