您好,登錄后才能下訂單哦!
index.py 這里只是假設一個模擬登陸
# -*- 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
# -*- 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
# -*- 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。