Python connection MySQL multi-tier structure instance

Source: Internet
Author: User


One, Python connection MySQL multilayer structure:

Catalog File Description:

SqlExec SQL to perform operations, for example: adding or deleting changes

Table database table Module One module

index.py Master File

conf.py Database connection Information

The directory structure is as follows:

-rw-r--r--1 root root 167 June 20:36 conf.py
-rw-r--r--1 root root 244 June 20:36 Conf.pyc
-rw-r--r--1 root root 594 June 20:35 index.py
Drwxr-xr-x 2 root root 4096 June 20:36 SqlExec
Drwxr-xr-x 2 root root 4096 June 20:39 table

./sqlexec:
Total 12
-rw-r--r--1 root root 0 June 18:48 __init__.py
-rw-r--r--1 root root 138 June 18:48 __init__.pyc
-rw-r--r--1 root root 911 June 20:21 sql_exec.py
-rw-r--r--1 root root 1690 June 20:21 Sql_exec.pyc

./table:
Total 12
-rw-r--r--1 root root 0 June 18:48 __init__.py
-rw-r--r--1 root root, June 18:48 __init__.pyc
-rw-r--r--1 root root 1246 June 19:57 users.py
-rw-r--r--1 root root 1782 June 19:57 Users.pyc


Second, the specific script content:

1, index.py

#!/usr/bin/python27
#coding: utf-8

Import Os,sys
from table.users import users

def main ():
    username = raw_input (' username: ')
    password = raw_input (' Password: ')
    check = Users ()
    result = Check.checkvalidate (Username,password)
     If not result:
        print (' User name password error ')
    else:
        print (' Welcome to Admin system ')
         User_list = check.get_dict_user_info (0, ')
        for key in User_list:
            for item in Key.items ():
                 print (item)


If __name_ _ = = ' __main__ ':
    os.system (' Clear ')
    main ()


2, conf.py

#!/usr/bin/python27
#coding =utf-8

Conf_dict = {' host ': ' 127.0.0.1 ',
' User ': ' Root ',
' passwd ': ' Root ',
' DB ': ' Yunwei '
}


3, table directory under users.py

#!/usr/bin/python27
#coding: Utf-8

Import Os,sys
Sys.path.append ("..")
From sqlexec.sql_exec import mysql_exec

Class users (Object):
def __init__ (self):
Self.__result = Mysql_exec ()

def get_dict_user_info (Self,flag,user):
If flag = = 1:
sql = "SELECT * from Users where user_name=%s"
res = self.__result.get_dict (sql,user)
Elif flag = = 0:
sql = "SELECT * from Users"
res = self.__result.get_dict (sql, ")
Else
res = "The flag is error"
return res


def get_one_user_info (Self,flag,user):
If flag = = 1:
sql = "SELECT * from Users where user_name=%s"
res = Self.__result.get_one (sql,user)
Elif flag = = 0:
sql = "SELECT * from Users"
res = self.__result.get_one (sql, ")
Else
res = "The flag is error"
return res

def checkvalidate (SELF,USER,PASSWD):
sql = "Select User_name,password from Users where user_name=%s and Password=md5 (%s)"
params = (user,passwd,)
res = Self.__result.get_one (sql,params)
return res


4, SqlExec directory under sql_exec.py

#!/usr/bin/python27
#coding: Utf-8

Import Os,sys
Import MySQLdb
Sys.path.append ('.. ')
Import conf

Class Mysql_exec (object):
def __init__ (self):
Self.__conn = Conf.conf_dict

def __connect (self):
Try
conn = MySQLdb.connect (**self.__conn)
Except Exception,e:
Print (e)
Sys.exit (' Connect failed ')

cur = conn.cursor (cursorclass=mysqldb.cursors.dictcursor)
Return cur
Cur.close ()
Conn.close ()

def get_dict (self,sql,params):
res = Self.__connect ()
If params! = ":
Res.execute (Sql,params)
Else
Res.execute (SQL)
data = Res.fetchall ()
Return data

def get_one (self,sql,params):
res = Self.__connect ()
If params! = ":
Res.execute (Sql,params)
Else
Res.execute (SQL)
data = Res.fetchone ()
Return data


This article is from the "Autumn Fairy tale" blog, please be sure to keep this source http://wushank.blog.51cto.com/3489095/1664211

Python connection MySQL multi-tier structure instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.