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()
Python连接mysql数据库
最新推荐文章于 2022-04-10 00:27:24 发布
本文介绍如何使用Python的MySQLdb模块连接数据库、创建表和插入数据。通过实例演示了创建学生表并成功插入一条记录的过程。
277

被折叠的 条评论
为什么被折叠?



