import MySQLdb
class dbutil(object):
def __init__(self,host,user,pwd,db):
self.host = host
self.user = user
self.pwd = pwd
self.db = db
self.database = MySQLdb.connect(self.host,self.user,self.pwd,self.db)
print 'dbutil init success!'
def createTable(self):
cur = self.database.cursor()
sql = "create table student(id int,name varchar(20),age int,sex varchar(20),school varchar(20))";
cur.execute(sql)
print 'Create table Success!';
def insert(self):
cur = self.database.cursor()
sql = "insert into student(id,name,age,sex,school) values(1,'zhang',20,'boy','njupt')"
cur.execute(sql)
print 'insert into table student success'
dbu = dbutil("localhost","root","thwl","python")
dbu.createTable()
dbu.insert()
转载于:https://my.oschina.net/zhanghongbin01/blog/547687