1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
import pymysql import sys from utils import logs
class Usermysql(object): """ 封装控制mysql数据的增删改查功能 """
def __init__(self): self.addr = 'localhost' self.dbname = your_db self.username = your_username self.passwd = your_passwd self.charset = 'utf8' self.con = ''
def db_init(self): try: con = pymysql.connect( host=self.addr, user=self.username, passwd=self.passwd, charset=self.charset) except: logs('数据库账号密码错误!') sys.exit() try: cur = con.cursor() cur.execute('create database %s character set utf8;' % self.dbname) con.commit()
except Exception as e: logs('%s已存在%s数据库' % (e,self.dbname)) cur.execute('use %s' % self.dbname) sql = """ CREATE TABLE if not exists `infos` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `mobile` varchar(18) NOT NULL DEFAULT '', `bio` varchar(50) NOT NULL DEFAULT '', `username` varchar(50) NOT NULL DEFAULT '', `getime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `logintime` DATETIME, `note` varchar(20) DEFAULT '', PRIMARY KEY (`Id`) ) ENGINE=MyISAM CHARSET=utf8 ROW_FORMAT=DYNAMIC; """ sql2 = """ CREATE TABLE if not exists `groups` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `groupname` varchar(30) NOT NULL DEFAULT '', `username` varchar(50) NOT NULL DEFAULT '', `big` varchar(50) NOT NULL DEFAULT '', `nickname` varchar(50) NOT NULL DEFAULT '', `getime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `note` varchar(20) DEFAULT '', PRIMARY KEY (`Id`) ) ENGINE=MyISAM CHARSET=utf8 ROW_FORMAT=DYNAMIC; """ sql3 = """ CREATE TABLE if not exists `regs` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `mobile` varchar(18) NOT NULL DEFAULT '', `telstate` varchar(10) NOT NULL DEFAULT '', `messtate` varchar(10) NOT NULL DEFAULT '', `peoplestatu` varchar(10) NOT NULL DEFAULT '', `firstname` varchar(30) NOT NULL DEFAULT '', `secname` varchar(30) NOT NULL DEFAULT '', `filename` varchar(50) NOT NULL DEFAULT '', `logintime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `note` varchar(20) DEFAULT '', PRIMARY KEY (`Id`) ) ENGINE=MyISAM CHARSET=utf8 ROW_FORMAT=DYNAMIC; """ cur.execute(sql) cur.execute(sql2) cur.execute(sql3) con.close() logs('数据库初始化成功!')
def db_login(self): try: con = pymysql.connect(host=self.addr, user=self.username, passwd=self.passwd, charset=self.charset, db=self.dbname) except: logs('数据库账号密码错误!') sys.exit() logs('数据库连接成功',types=2) self.con = con
def db_add(self, tablename, colname, value): try: cur = self.con.cursor() cur.execute('insert into %s(%s) value(\'%s\')' % (tablename, colname, value)) self.con.commit() except Exception as e: logs('%s插入数据错误'%e)
def db_del(self, tablename, colname, value): try: cur = self.con.cursor() cur.execute('delete from %s where %s = \'%s\'' % (tablename, colname, value)) self.con.commit() except Exception as e: logs('%s删除数据错误'%e)
def db_update(self, tablename, colname, value, mobile): try: cur = self.con.cursor() cur.execute('update %s set %s = \'%s\' where mobile =\'%s\'' % (tablename, colname, value, mobile)) self.con.commit() except Exception as e: logs('%s修改数据错误'%e)
def db_select(self, tablename, colname, value, types=2): try: cur = self.con.cursor() cur.execute('select * from %s where %s =\'%s\'' % (tablename, colname, value)) if types == 2: ret = cur.fetchone() else: ret = cur.fetchall() return ret except Exception as e: logs('查询数据错误%s'%e)
def db_diysql(self, sql): try: cur = self.con.cursor() cur.execute(sql) self.con.commit() except Exception as e: logs('命令执行错误')
|