首页 技术 正文
技术 2022年11月9日
0 收藏 320 点赞 4,625 浏览 2564 个字

封装

  • 观察前面的文件发现,除了sql语句及参数不同,其它语句都是一样的
  • 创建MysqlHelper.py文件,定义类
#encoding=utf8
import MySQLdbclass 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

添加

  • 创建testInsertWrap.py文件,使用封装好的帮助类完成插入操作
#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'

查询一个

  • 创建testGetOneWrap.py文件,使用封装好的帮助类完成查询最新一行数据操作
#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

实例:用户登录

创建用户表userinfos

  • 表结构如下

    • id
    • uname
    • upwd
    • isdelete
  • 注意:需要对密码进行加密
  • 如果使用md5加密,则密码包含32个字符
  • 如果使用sha1加密,则密码包含40个字符,推荐使用这种方式
create table userinfos(
id int primary key auto_increment,
uname varchar(20),
upwd char(40),
isdelete bit default 0
);

加入测试数据

  • 插入如下数据,用户名为123,密码为123,这是sha1加密后的值
insert into userinfos values(0,'','40bd001563085fc35165329ea1ff5c5ecbdbbeef',0);

接收输入并验证

  • 创建testLogin.py文件,引入hashlib模块、MysqlHelper模块
  • 接收输入
  • 根据用户名查询,如果未查到则提示用户名不存在
  • 如果查到则匹配密码是否相等,如果相等则提示登录成功
  • 如果不相等则提示密码错误
#encoding=utf-8
from MysqlHelper import MysqlHelper
from hashlib import sha1sname=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 '密码错误'
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289