zoukankan      html  css  js  c++  java
  • MySQLdb与sqlalchemy的简单封装

    一:MySQLdb

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import MySQLdb
    import MySQLdb.cursors
    
    import configuration
    
    class DataAccess :
        
        conn = None
        cursor = None
        isClosed = True
    
        def open(self,host=configuration.host,port=configuration.port,db=configuration.db,user=configuration.user,pwd=configuration.pwd) :
            
            self.conn = MySQLdb.connect(host=configuration.host,port=configuration.port,db=configuration.db,user=configuration.user,passwd=configuration.pwd,charset="utf8")
            self.cursor = self.conn.cursor()
            self.isClosed = False
    
        def execute(self,cmd) :
            if self.isClosed :
                raise Exception("db is not opened!")
            ret = self.cursor.execute(cmd)
            return ret
    
        def queryList(self,cmd) :
            if self.isClosed :
                raise Exception("db is not opened!")
    
            self.cursor.execute(cmd)
            rows = self.cursor.fetchall()
    
            return rows
    
    
        def commit(self):
            self.conn.commit()
        
        def rolback(self):
            self.conn.rolback()
    
        def close(self) :
            if self.isClosed :
                pass;
            
            self.conn.close();
            self.isClosed=True;

    二:sqlalchemy

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    from sqlalchemy import *
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.orm import Session
    from sqlalchemy.ext.declarative import declarative_base
    
    import configuration
    
    class Persister(object):
        
        session = None;
        isClosed = True;
    
        def open(self,host=configuration.host,port=configuration.port,db=configuration.db,user=configuration.user,pwd=configuration.pwd) :
            
            url = 'mysql+mysqlconnector://%s:%s@%s:%d/%s' % (user,pwd,host,port,db)
            engine = create_engine(url)
            DbSession = sessionmaker(bind=engine)
    
            self.session = DbSession()
    
            self.isClosed = False
    
            return self.session
    
        def query(self,type) :
            
            query = self.session.query(type)
            return query
    
        def add(self,item) :
            self.session.add(item)        
    
        def add_all(self,items) :
            self.session.add_all(items)
    
        def delete(self,item) :
            self.session.delete(item)
    
        def commit(self) :
            self.session.commit()
    
        def close(self) :
            
            if self.isClosed :
                pass
            
            self.session.close()
            self.isClosed = True

    三:测试代码

    # !/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    from sqlalchemy import *
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.orm import Session
    from sqlalchemy.ext.declarative import declarative_base
    
    from entity import *
    from utils.SSH import *
    from dao.persister import *
    
    from dao.dao import *
    
    persister = Persister()
    persister.open()
    query = persister.query(w_node)
    nodes = query.filter(w_node.master==False).all()
    
    print len(nodes)
    
    
    dao = DataAccess()
    dao.open()
    
    rows = dao.queryList("select * from w_node")
    for row in rows :
        print row[0],row[1],row[2],row[3],row[4]
    
    dao.close()
    persister.close()

    作者    :秋时

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

  • 相关阅读:
    0127 date dateformat calebdar
    0126 字符串缓冲区StringBuffer类 正则表达式
    0126 String类
    0125 java API object
    0125 匿名对象 内部类 包 代码块
    0123 final关键字,static 关键字
    0123 this关键字 super关键字
    0122面向对象 构造方法
    0122面向对象3 多态
    0120 面向对象2
  • 原文地址:https://www.cnblogs.com/Netsharp/p/8379448.html
Copyright © 2011-2022 走看看