python mysqldb的编写
来源:互联网 发布:telnet端口 不通 编辑:程序博客网 时间:2024/05/22 12:39
#!/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 MySQLdbimport string,randomLEN = 5NUM = 200def 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 listclass 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()
0 0
- python mysqldb的编写
- python的MySqldb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- Python的MYSQLdb模块安装
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- python下的MySQLdb使用
- Python的MySQLdb模块安装
- iOS枚举的使用
- Struts2拦截器校验
- 网络层其它协议
- [leetcode 463]Island Perimeter
- 使用jquery.qrcode.js生成二维码
- python mysqldb的编写
- ubuntu上安装dlib
- 优先级继承
- 1044. 火星数字(20)
- 微信静默授权,利用花生壳在本地测试
- 分享 CT reconstruction
- json解析
- 传输层:UDP协议
- eclipse调优