#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    15

    Inserting values into MySQL table


    Hello,

    I am using the Python MySQL module to create a table and insert values into it.
    I use the following code:
    Code:
    cursor.execute ("""
           CREATE TABLE animal
           (
               name CHAR(40),
               category CHAR(40)
           )
       """)
    
    cursor.execute ("""
           INSERT INTO animal (name, category)
           VALUES
               ('snake', 'reptile'),
               ('frog', 'amphibian'),
               ('tuna', 'fish'),
               ('racoon', 'mammal')
       """)
    
    cursor.execute ("SELECT name, category FROM animal")
    while (1):
       row = cursor.fetchone ()
       if row == None:
           break
       print "%s, %s" % (row[0], row[1])
    which shows the results;
    Code:
    snake, reptile
    frog, amphibian
    tuna, fish
    racoon, mammal
    However, when I look at the table using the MySQL command prompt, the table is created but it's empty. What could be the reason? Is it a temporary insertion that my code does?

    Thanks for your help.
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    No such thing as a temporary insertion, and that all looks fine to me. Double check you're looking at the same database and directory.

    --Simon

IMN logo majestic logo threadwatch logo seochat tools logo