#!/usr/bin/env python
# coding=utf-8
import MySQLdb
def connectdb():
print('连接到mysql服务器...')
# 打开数据库连接
# 用户名:root, 密码:root.,用户名和密码需要改成你自己的mysql用户名和密码,并且要创建数据库TEST,并在TEST数据库中创建好表Student
db = MySQLdb.connect("localhost","root","root","test")
print('连接上了!')
return db
#删除表
def dropteble(db,sql):
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 如果存在表Sutdent先删除
cursor.execute(sql)
#创建表
def createtable(db,sql):
# 使用cursor()方法获取操作游标
cursor = db.cursor()
cursor.execute(sql)
#插入数据
def insertdb(db,sql):
# 使用cursor()方法获取操作游标
cursor = db.cursor()
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
print '插入数据成功!&#