Newbee - MySQL create database/table

Skip Montanaro skip at pobox.com
Sat Sep 15 11:12:18 EDT 2001


    Danny> I wonder if someone could put here or send me a sample Python
    Danny> scrip on how to create a database and how to create a table using
    Danny> MySQL with MySQLdb. 

Danny,

I don't need to create databases or tables on-the-fly, so I create them
ahead of time with a sql script I feed directly to the mysql program, but
something like this ought to work:

    db = MySQLdb.Connection(host=host,
                            user=user,
                            passwd=password,
                            db=database)
    c = db.cursor()
    # create a database
    c.execute("create database concerts")
    # make it the current db
    c.execute("use concerts")
    # add a table named evperf
    c.execute("""create table evperf (
      id int unsigned DEFAULT 0 NOT NULL AUTO_INCREMENT,
      event int unsigned DEFAULT 0 NOT NULL,
      performer int unsigned DEFAULT 0 NOT NULL,
      unique id (id),
      key event (event),
      key performer (performer)
    )""")

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list