index.py 這里只是假設(shè)一個(gè)模擬登陸
# -*- 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 '用戶(hù)不存在,請(qǐng)重新登錄' else: print '登錄成功' if __name__ == "__main__": main()user.py
# -*- coding: utf-8 -*- """ Created on Sun Nov 27 19:48:03 2016 對(duì)數(shù)據(jù)庫(kù)表的處理,py文件名和表名一一對(duì)應(yīng) 所以,在這里的user.py文件是對(duì)數(shù)據(jù)庫(kù)表為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 數(shù)據(jù)處理層,處理數(shù)據(jù)的最底層,例如增刪改查的功能 @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()獲取所有數(shù)據(jù) 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()是獲取一條數(shù)據(jù) self.cur.close() self.conn.close() return data另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。