Python操作操作mysql数据库实现增删查改功能的方法数据库实现增删查改功能的方法
主要介绍了Python操作mysql数据库实现增删查改功能的方法,涉及Python针对mysql数据库的连接、增删改查等相关操作技巧,需要的朋友可以参考下
本文实例讲述了Python操作mysql数据库实现增删查改功能的方法。分享给大家供大家参考,具体如下:
#coding=utf-8
import MySQLdb
class Mysql_Oper:
def __init__(self,host,user,passwd,db):
self.host=host
self.user=user
self.passwd=passwd
self.database=db
def db_connecet(self):
try:
#连接
conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.passwd,db=self.database,charset="utf8")
cursor = conn.cursor()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
def drop_table(self,table):
try:
#删除表
sql = "drop table if exists" + table
cursor.execute(sql)
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
def create_table(self,table):
try:
if table=="dept":
#创建
sql = "create table if not exists dept(deptno int primary key, dname varchar(50),loc varchar(50))"
cursor.execute(sql)
elif table=="emp":
sql == "create table if not exists emp(empno INT PRIMARY KEY,ename VARCHAR(50),job VARCHAR(50),mgr INT,hiredate DATE,sal DECIMAL(7,2),COMM DECIMAL(7,2),deptno INT,loc varchar(50),CONSTRAINT fk_emp FOREIGN KEY(mgr) REFERENCES emp(empno))"
cursor.execute(sql)
elif table=="salgrade":
sql = "create table if not exists salgrade(grade INT PRIMARY KEY,losal INT,hisal INT)"
cursor.execute(sql)
elif table=="stu":
#创建
sql = "create table if not exists dept(sid INT PRIMARY KEY,sname VARCHAR(50),age INT,gander VARCHAR(10),province VARCHAR(50),tuition INT)"
cursor.execute(sql)
else:
print u"输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
def inser_onedata_table(self,table):
try:
if table=="dept":
sql = "insert into dept values(%s,%s,%s)"
param = (40, 'cai wu bu', 'wu han')
n = cursor.execute(sql,param)
print 'insert',n
elif table=="emp":
sql = "insert into emp values(%s,%s,%s,%s,%s,%s,%s,%s)"
param = (1009, 'a niu', 'dong shi zhang', NULL, '2001-11-17', 50000, NULL, 10)
n = cursor.execute(sql,param)
print 'insert',n
elif table=="salgrade":
sql = "insert into salgrade values(%s,%s,%s)"
param = (1, 7000, 12000)
n = cursor.execute(sql,param)
print 'insert',n
elif table=="stu":
sql = "insert into stu values(%s,%s,%s,%s,%s,%s)"
param = ('1', '001', '23', 'nan', 'bei jing', '1500')
n = cursor.execute(sql,param)
print 'insert',n
else:
print u"输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
def inser_muldata_table(self,table):
try:
if table=="dept":
sql = "insert into dept values(%s,%s,%s)"
param = ((10, 'jiao yan bu', 'bei jing'),(20, 'xue gong bu', 'shang hai'),(30, 'xiao shou bu', 'guang zhou'))
n = cursor.executemany(sql,param)
print 'insert',n
elif table=="emp":
sql = "insert into emp values(%s,%s,%s,%s,%s,%s,%s,%s)"
param = ((1004, 'liu bei', 'jing li', 1009, '2001-04-02', 29750, NULL, 20),
(1006, 'guan yu', 'jing li', 1009, '2001-05-01', 28500, NULL, 30),
(1008, 'zhu ge liang', 'fen xi shi', 1004, '2007-04-19', 30000, NULL, 20),
(1013, 'pang', 'fen xi shi', 1004, '2001-12-03', 30000, NULL, 20),
(1002, 'dai', 'xiao shou yuan', 1006, '2001-02-20', 16000, 3000, 30),
(1003, 'tian zheng', 'xiao shou yuan', 1006, '2001-02-22', 12500, 5000, 30),
(1005, 'xie xun', 'xiao shou yuan', 1006, '2001-09-28', 12500, 14000, 30),
(1010, 'wei yi xiao', 'xiao shou yuan', 1006, '2001-09-08', 15000, 0, 30)
)
n = cursor.executemany(sql,param)
print 'insert',n
elif table=="salgrade":
sql = "insert into salgrade values(%s,%s,%s)"
param = ((2, 12010, 14000),(3, 14010, 20000),(4, 20010, 30000),(5, 30010, 99990))
n = cursor.executemany(sql,param)
print 'insert',n
elif table=="stu":
sql = "insert into stu values(%s,%s,%s,%s,%s,%s)"
param = ( ('2', '002', '25', 'nan', 'liao ning', '2500'),
('3', '003', '22', 'nan', 'bei jing', '3500'),
('4', '004', '25', 'nan', 'bei jing', '1500'),
('5', '005', '23', 'nv', 'bei jing', '1000'),
('6', '006', '22', 'nv', 'shan dong', '2500'),
('7', '007', '21', 'nv', 'bei jing', '1600'),
('8', '008', '23', 'nan', 'bei jing', '3500'),
('9', '009', '23', 'nv', 'guang zhou', '2500'),
('10', '010', '18', 'nan', 'shan xi', '3500'),
('11', '011', '23', 'nan', 'hu bei', '4500'),
('12', '011', '24', 'nan', 'bei jing', '1500'),
('13', '011', '24', 'nan', 'liao ning', '2500'),
('14', '011', '22', 'nan', 'bei jing', '3500'),
('15', '011', '25', 'nan', 'bei jing', '1500'),
('16', '011', '23', 'nv', 'bei jing', '1000'),
('17', '011', '22', 'nv', 'shan dong', '2500'),
('18', '011', '21', 'nv', 'bei jing', '1600'),
('19', '011', '23', 'nan', 'bei jing', '3500'),
('20', '011', '23', 'nv', 'guang zhou', '2500'),
('21', '011', '18', 'nan', 'shan xi', '3500'),
('22', '011', '23', 'nan', 'hu bei', '4500'),
('23', '011', '23', 'nan', 'bei jing', '1500'),
('24', '011', '25', 'nan', 'liao ning', '2500'),
('25', '011', '22', 'nan', 'bei jing', '3500'),
('26', '011', '25', 'nan', 'bei jing', '1500'),
('27', '011', '23', 'nv', 'bei jing', '1000'),
('28', '011', '22', 'nv', 'shan dong', '2500'),