python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python sqlalchemy连接数据库

Python使用sqlalchemy实现连接数据库的帮助类

作者:搬砖的诗人Z

这篇文章主要为大家详细介绍了Python如何使用sqlalchemy实现连接数据库的帮助类,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考下

python使用 sqlalchemy连接数据库帮助类

实现代码

import mysql.connector

class MySqlHelper(object):
    """操作数据库帮助类"""

    def __init__(self):
        #self.host = "localhost"
        #self.user = "root"
        #self.password = "xinshiyun@123"
        #self.database = "deliverunion_callcenter"
        self.host = "192.168.60.156"
        self.user = "root"
        self.password = "root"
        self.database = "deliverunion_callcenter"
        try:
            self.mydb = mysql.connector.connect(host=self.host,
                        user=self.user,
                        passwd=self.password,
                        database=self.database,
                        connect_timeout=10)
                        #database=self.database,
                        #auth_plugin='mysql_native_password')
            self.mycursor = self.mydb.cursor()
        except Exception as e:
            print('MySql Error : %d %s' % (e.args[0],e.args[1]))


    #不带参数的查询
    def select(self,mysql):
        try:
            self.mycursor.execute(mysql)
            values = self.mycursor.fetchall()
            return values
        except Exception as e:
            print ('select Error : %d %s' % (e.args[0],e.args[1]))
            return []
        finally:
            self.mycursor.close()
            self.mydb.close()

    #带参数的查询
    def select2(self,mysql,na):
        try:
            self.mycursor.execute(mysql,na)
            values = self.mycursor.fetchall()
            return values
        except Exception as e:
            print ('select2 Error : %d %s' % (e.args[0],e.args[1]))
            return []
        finally:
            self.mycursor.close()
            self.mydb.close()

    #更新
    def Update(self,mysql,na):
         try:
            self.mycursor.execute(mysql,na)
            self.mydb.commit()
            row = self.mycursor.rowcount
            if row > 0:
                return True
            else:
                return False
         except Exception as e:
            print ('Update Error : %d %s' % (e.args[0],e.args[1]))
            return False
          
         finally:
            self.mycursor.close()
            self.mydb.close()

    #插入数据
    def Insert(self,mysql,na):
         try:
            self.mycursor.execute(mysql,na)
            self.mydb.commit()
            row = self.mycursor.rowcount
            if row > 0:
                return True
            else:
                return False
         except Exception as e:
            print('Insert Error : %d %s' % (e.args[0],e.args[1]))
            return False
          
         finally:
            self.mycursor.close()
            self.mydb.close()

使用数据:

from DAL import MySqlHelper
from Entity import TaskPoolEntity
import datetime

class TaskPoolDAL(object):
    """操作数据库t_du_guiji_task"""

    sqlHelper = MySqlHelper.MySqlHelper()

    #查询所有的任务
    def selectTasks():
        TaskPoolDAL.sqlHelper = MySqlHelper.MySqlHelper()
        sql='select * from  t_du_guiji_task'
        values= TaskPoolDAL.sqlHelper.select(sql)
        data=[]
        for item in values:
           selectTask = TaskPoolEntity.TaskPoolEntity(item)
           data.append(selectTask)

        return data

以上就是Python使用sqlalchemy实现连接数据库的帮助类的详细内容,更多关于Python sqlalchemy连接数据库的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文