Utility  :对数据库的操作,定义增删改查连接。

Module : 定义处理数据库的方法,每个表建立一个类,再定义处理表的操作方法,sql语句在这里编写。

Index : 定义逻辑  

Config 定义常量(比如数据库连接字符串)

MysqlHelper.py

#!/usr/bin/python
#_*_ coding:UTF-8 _*_
import config
import pymysql
class MysqlHelper(object):
    def __init__(self):
        self._conn_dict = config.conn_string


    def Get_Dict(self,sql,params):
        conn = pymysql.connect(**self._conn_dict)
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql,params)
        data = cur.fetchall()
        return data
        cur.close()
        conn.close()

    def Get_one(self,sql,params):
        # * list ** dict
        conn = pymysql.connect(**self._conn_dict)
        #cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur = conn.cursor()
        cur.execute(sql,params)
        data = cur.fetchone()
        return data
        cur.close()
        conn.close()

    #  insert data:
    def insert_data(self,sql,params):
        conn = pymysql.connect(**self._conn_dict)
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql,params)
        conn.commit()
        cur.close()
        conn.close()

    #update data
    def update_data(self,sql,params):
        conn = pymysql.connect(**self._conn_dict)
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql, params)
        conn.commit()
        cur.close()
        conn.close()

    # delete data:
    def deledata(self,sql,params):
        conn = pymysql.connect(**self._conn_dict)
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql, params)
        conn.commit()
        cur.close()
        conn.close()




config.py

#!/usr/bin/python
#_*_ coding:UTF-8 _*_

conn_string = dict(host='192.168.1.113',user='root',passwd='123456',db='admin')

Admin.py

#!/usr/bin/python
#_*_ coding:UTF-8 _*_

from utility.MysqlHelper import MysqlHelper
class Admin(object):
    def __init__(self):
        self._helper = MysqlHelper()
    def get_name(self,id):
        sql = 'select * from admin WHERE  id = %s'
        params = (id,)
        return self.__helper.Get_one(sql,params)

    def checkadmin(self,username,passwd):
        sql = 'select * from admin WHERE  name = %s and passwd = %s'
        params = (username,passwd,)
        return self._helper.Get_one(sql,params)

    # update passwd

    def update_passwd(self,username,passwd):
        sql = 'update admin set passwd=%s WHERE name=%s'
        params = (passwd,username)
        result = self._helper.update_data(sql,params)
        if result:
            print('update password sucessfull!!')


index.py

#!/usr/bin/python
#_*_ coding:UTF-8 _*_

from model.Admin import Admin
def main():

    user = input("input username:")
    passwd = input("input password:")
    admin = Admin()
    result = admin.checkadmin(user,passwd)
    print(result)
    if not result:
        print("The user is not exist")

    '''
    print("update user's password")word:")
    admin = Admin()
    admin.upd
    user = input("input username:")
    passwd = input("input passate_passwd(user,passwd)
    '''






if __name__ == '__main__':
    main()

 

 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐