index.py 这里只是假设一个模拟登陆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # -*- coding: utf-8 -*- """ Created on Sun Nov 27 18:54:29 2016 这是主程序文件 @author: toby """ from model.user import User def main(): username = "tantianran1" user = User() result = user.Check_Username(username) if not result: print '用户不存在,请重新登录' else : print '登录成功' if __name__ = = "__main__" : main() |
user.py
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 | # -*- coding: utf-8 -*- """ Created on Sun Nov 27 19:48:03 2016 对数据库表的处理,py文件名和表名一一对应 所以,在这里的user.py文件是对数据库表为user的处理 @author: toby """ import sys sys.path.append( "/home/toby/workspace/date20161128" ) from utility.sql_helper import MysqlHelper class User( object ): def __init__( self ): self .__helper = MysqlHelper() def Get_data_by_id( self ,ids): sql = "select * from user where id=%s" params = (ids,) return self .__helper.Get_One_Data(sql,params) def Check_Username( self ,name): sql = "select * from user where name=%s" params = (name,) return self .__helper.Get_One_Data(sql,params) ''' a = User() print a.Check_Username('tantianran') ''' |
sql_helper.py
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 | # -*- coding: utf-8 -*- """ Created on Sun Nov 27 18:57:44 2016 数据处理层,处理数据的最底层,例如增删改查的功能 @author: toby """ import MySQLdb class MysqlHelper( object ): def __init__( self ): hosts,users,password,dbname = '127.0.0.1' , 'root' , '1qaz#EDC' , 'test_db' self .conn = MySQLdb.connect(host = hosts,user = users,passwd = password,db = dbname) self .cur = self .conn.cursor(MySQLdb.cursors.DictCursor) def Get_Dict_data( self ,sql,params): self .cur.execute(sql,params) data = self .cur.fetchall() #fetchall()获取所有数据 self .cur.close() self .conn.close() return data def Get_One_Data( self ,sql,params): self .cur.execute(sql,params) data = self .cur.fetchone() #fetchone()是获取一条数据 self .cur.close() self .conn.close() return data |