标签:select user 引入 文件 cep esc 用户 bee 测试
#encoding=utf8 import MySQLdb class MysqlHelper(): def __init__(self,host,port,db,user,passwd,charset=‘utf8‘): self.host=host self.port=port self.db=db self.user=user self.passwd=passwd self.charset=charset def connect(self): self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset) self.cursor=self.conn.cursor() def close(self): self.cursor.close() self.conn.close() def get_one(self,sql,params=()): result=None try: self.connect() self.cursor.execute(sql, params) result = self.cursor.fetchone() self.close() except Exception, e: print e.message return result def get_all(self,sql,params=()): list=() try: self.connect() self.cursor.execute(sql,params) list=self.cursor.fetchall() self.close() except Exception,e: print e.message return list def insert(self,sql,params=()): return self.__edit(sql,params) def update(self, sql, params=()): return self.__edit(sql, params) def delete(self, sql, params=()): return self.__edit(sql, params) def __edit(self,sql,params): count=0 try: self.connect() count=self.cursor.execute(sql,params) self.conn.commit() self.close() except Exception,e: print e.message return count
#encoding=utf8 from MysqlHelper import * sql=‘insert into students(sname,gender) values(%s,%s)‘ sname=raw_input("请输入用户名:") gender=raw_input("请输入性别,1为男,0为女") params=[sname,bool(gender)] mysqlHelper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘) count=mysqlHelper.insert(sql,params) if count==1: print ‘ok‘ else: print ‘error‘
#encoding=utf8 from MysqlHelper import * sql=‘select sname,gender from students order by id desc‘ helper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘) one=helper.get_one(sql) print one
create table userinfos( id int primary key auto_increment, uname varchar(20), upwd char(40), isdelete bit default 0 );
insert into userinfos values(0,‘123‘,‘40bd001563085fc35165329ea1ff5c5ecbdbbeef‘,0);
#encoding=utf-8 from MysqlHelper import MysqlHelper from hashlib import sha1 sname=raw_input("请输入用户名:") spwd=raw_input("请输入密码:") s1=sha1() s1.update(spwd) spwdSha1=s1.hexdigest() sql="select upwd from userinfos where uname=%s" params=[sname] sqlhelper=MysqlHelper(‘localhost‘,3306,‘test1‘,‘root‘,‘mysql‘) userinfo=sqlhelper.get_one(sql,params) if userinfo==None: print ‘用户名错误‘ elif userinfo[0]==spwdSha1: print ‘登录成功‘ else: print ‘密码错误‘
标签:select user 引入 文件 cep esc 用户 bee 测试
原文地址:https://www.cnblogs.com/leecoffee/p/9039008.html