#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = '' __author__ = 'zhubin' __mtime__ = '2016/12/7' # code is far away from bugs with the god animal protecting I love animals. They taste delicious. ┏┓ ┏┓ ┏┛┻━━━┛┻┓ ┃ ☃ ┃ ┃ ┳┛ ┗┳ ┃ ┃ ┻ ┃ ┗━┓ ┏━┛ ┃ ┗━━━┓ ┃ 神兽保佑 ┣┓ ┃ 永无BUG! ┏┛ ┗┓┓┏━┳┓┏┛ ┃┫┫ ┃┫┫ ┗┻┛ ┗┻┛ """ import MySQLdb import string,random LEN = 5 NUM = 200 def getRandom(len = LEN,num = NUM): allChar = string.digits+string.letters list =[] for i in range(num): al =[random.choice(allChar) for m in range(len)] st = "".join(al) list.append(st) return list class SQLDb: TABLENAME = 'code' def __init__(self): self.connect() def connect(self): self.conn = MySQLdb.Connect(host='localhost',port=3306,passwd='123456',user='zbb',charset='utf8') self.conn.select_db("test") def getCursor(self): if self.conn: try: return self.conn.cursor() except: self.connect() return self.conn.cursor() print "cann't get cursor" def commit(self): if self.getCursor(): self.conn.commit() def closeDb(self): self.conn.close() def createTable(self): # try: exist = self.isExist() if not exist: sql = "CREATE TABLE IF NOT EXISTS "+ self.TABLENAME +"( code char(20))" self.getCursor().execute(sql) # except : # self.conn.rollback() # print "table has exists" def isExist(self): exist = False cur = self.getCursor() cur.execute("show tables in test;") tables = cur.fetchall() # print tables for table in tables: if 'code' in table: exist = True # print exist return exist def insertMany(self,list): try: sql ="INSERT INTO "+ self.TABLENAME +"(code)VALUES(%s)" print sql self.getCursor().executemany(sql,list) self.commit() except: print 'insert many failed' def insertOne (self,va): # try: sql = 'INSERT INTO '+self.TABLENAME+ " (code) VALUES ('%s')"%va print sql self.getCursor().execute(sql) self.commit() # except: # print 'insert one failed' def delete(self,code): # try: sql = "DELETE FROM "+ self.TABLENAME+" WHERE code='%s'"%code self.getCursor().execute(sql) self.commit()#必须的 # except: # print 'del failed' def select(self): # try: sql = "select * from "+self.TABLENAME print sql cur = self.getCursor() cur.execute(sql) resultes = cur.fetchall() for result in resultes: print result # self.commit() # except: # print 'select exception' def update(self,code): # try: sql ="UPDATE "+self.TABLENAME +" SET code = '%s'"%code+" WHERE code='sWfhK'" self.getCursor().execute(sql) self.commit() # except: # print 'update uxception' def alter(self,columName,columType): # try: sql ="ALTER TABLE "+self.TABLENAME +" ADD "+ columName+ " "+columType self.getCursor().execute(sql) # except: # print 'colum has exists' def drop(self): try: sql = "drop TABLE IF EXISTS "+self.TABLENAME self.getCursor().execute(sql) except: print 'drop exception' def getCount(self): try: sql ="select * from "+self.TABLENAME return self.getCursor().execute(sql) except: print "getcount failed" def checkVersion(self): self.conn.query("SELECT VERSION()") result = self.conn.use_result() print 'sql version:%s'%result.fetch_row()[0] if __name__ =="__main__": sql = SQLDb() sql.createTable() list = getRandom() # sql.select() # sql.delete('ABCD') # sql.update('ABCD') # sql.alter("id4","INT not NULL") # sql.insertMany(list) # sql.insertOne("ABCD") print sql.getCount() # sql.drop()
python mysqldb的编写
最新推荐文章于 2021-01-30 21:10:39 发布