python 连接mysql多层结构实例

一、python 连接mysql多层结构:

目录文件介绍:

sqlexec             sql执行操作,例:增删改查

table                 数据库表模块 一个表一个模块

index.py            主文件

conf.py              数据库连接信息

目录结构如下:



-rw-r--r-- 1 root root     167 Jun 22 20:36 conf.py
-rw-r--r-- 1 root root     244 Jun 22 20:36 conf.pyc
-rw-r--r-- 1 root root     594 Jun 22 20:35 index.py
drwxr-xr-x 2 root root    4096 Jun 22 20:36 sqlexec
drwxr-xr-x 2 root root    4096 Jun 22 20:39 table

./sqlexec:
total 12
-rw-r--r-- 1 root root    0 Jun 22 18:48 __init__.py
-rw-r--r-- 1 root root  138 Jun 22 18:48 __init__.pyc
-rw-r--r-- 1 root root  911 Jun 22 20:21 sql_exec.py
-rw-r--r-- 1 root root 1690 Jun 22 20:21 sql_exec.pyc

./table:
total 12
-rw-r--r-- 1 root root    0 Jun 22 18:48 __init__.py
-rw-r--r-- 1 root root  130 Jun 22 18:48 __init__.pyc
-rw-r--r-- 1 root root 1246 Jun 22 19:57 users.py
-rw-r--r-- 1 root root 1782 Jun 22 19:57 users.pyc



二、具体脚本内容:

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(‘用户名密码错误‘)
    else:
        print(‘欢迎登录后台管理系统‘)
        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目录下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目录下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



本文出自 “秋天的童话” 博客,请务必保留此出处http://wushank.blog.51cto.com/3489095/1664211

时间: 2024-08-24 12:19:15

python 连接mysql多层结构实例的相关文章

Python连接MySQL的实例代码

Python连接MySQL的实例代码 MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME%/Lib/site-packages目录中,python会自动找到此包. MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0. 其他: 1. 平台及版本 linux 内核2.6,gcc 3.4.4,glibc 2

Python操作MySQL数据库实例

在Windows平台上安装mysql模块用于Python开发 用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示.下边是打包了32与64版本. MySQL-python-1.2.3.win32-py2.7.exe MySQL-python-1.2.3.win-amd64-py2.7.exe 免费下载地址:http://yunpan.cn/cVnTc9iRsQR4m 访问密码 06fc 安装过程很简单不多说: 实例 1.取得 MYSQL 的版本 # -*- coding:

Python连接Mysql数据库(Debian)

Python连接Mysql数据库(Debian) 以下是Python 2.*版本的安装方法,MySQL-python暂不支持Python 3.*版本 提前要做的工作: 安装setuptools,在终端中运行 wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python 安装pip,下载“get_pip.py”,运行 python get_pip.py 运行如下命令装好必要的包 sudo apt-get install python-d

[笔记]--在Ubuntu系统用Python连接Mysql数据库

环境:Ubuntu11.10,Python2.7,Mysql5.0.95 在Ubuntu终端输入命令安装Python的Mysql模块 sudo apt-get install python-mysqldb 就这么简单: 运行一下脚本: #!/usr/bin/python #-*-coding=utf-8# # import MySQLdb cn = MySQLdb.Connection(host="192.168.88.124",user="root",passwd

Python连接MySQL数据库之pymysql模块使用

Python连接MySQL数据库之pymysql模块使用 Python3连接MySQL PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQL数据库. PyMySQL安装 pip install pymysql 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可以连接该数据库的用户名和密码 你有一个有权限操作的datab

python入门(十七)python连接mysql数据库

mysql 数据库:关系型数据库mysql:互联网公司 sqllite:小型数据库,占用资源少,手机里面使用oracle:银行.保险.以前外企.sybase:银行+通信 互联网公司key:valuemongodb:磁盘上redis:内存数据库,持久化memchache:内存数据库 mysql -uroot -p密码装完了之后,cmd下输入mysql命令,需要将安装目录下的bin目录( mysql.exe 所在的目录)加入到path中 本地连接 mysql -uroot -p mysql -h12

python连接mysql操作(1)

python连接mysql操作(1) import pymysql import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='10.10.146.28', port=3306, user='admin_m', passwd='fcfmTbRw1tz2x5L5GvjJ', db='test', charset='utf8mb4' ) def create_table(): cursor = connect.cursor() #

寒假学习进度-6(Python连接MySQL数据库)

Python连接mysql和操作 软件:pycharm 开始在pycharm下面的Terminal中安装mysql时提醒pip版本不够,所以需要先升级一下pip python -m pip install --upgrade pip 升级完pip之后就可以下载mysql pip install mysql 下载完成后在setting中查看 进行代码测试 #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db =MySQLdb.con

python连接mysql数据库——版本问题

今天终于解决了使用python连接数据库不成功的问题,现将过程总结如下: 一.出现的问题 在使用python连接mysql数据库是一直出现如下问题: 1.只能连接到我数据库中的的第一个数据库,但是不能操作里面的表,会报错表不存在.(表是存在的)2.更换其他数据库后,直接报错找不到该数据库.(数据库和表均存在) 运行连接数据库的代码,会出现: conn = pymysql.connect(user='root', password='password', database='XXX') Trace