http://meetbill.github.io/
分类: LINUX
2016-03-13 23:21:46
python自动创建数据库以及数据库中的表
import MySQLdb conn = MySQLdb.connect(host = 'localhost', port = 3306, user = 'root', passwd = '*****') curs = conn.cursor()
try: curs.execute('create database addtest') except: print 'Database addtest exists!' conn.select_db('addtest') # create a table named addfields
try: curs.execute('create table addfields(id int PRIMARY KEY NOT NULL,name text)') except: print('The table addfields exists!') # add the fileds
curs.close()
conn.close()