Python公共类-获取MySQL数据
Posted 巧克力脆片
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python公共类-获取MySQL数据相关的知识,希望对你有一定的参考价值。
# -*- coding: UTF8 -*- __author__ = ‘zhangh‘ import pymysql class Conn2MySQL(object): def __init__(self, host, user, password, port): """ connect to mysql get mysql data :return: tuple """ self.host = host self.user = user self.port = port self.passwd = password def get_data(self, sql): connection = pymysql.connect(host=self.host, user =self.user, passwd=self.passwd, port=self.port, charset="utf8") try: with connection.cursor() as cursor: cursor.execute(sql) result = cursor.fetchall() connection.commit() return result except pymysql.Error as e: return e finally: connection.close() # sql = "select id from dbcm.tmp where id=1" # conn = Conn2MySQL(host=‘172.19.44.12‘, user=‘mysqladmin‘, password=‘mysqladmin‘, port=3306) # result = conn.get_data(sql)
以上是关于Python公共类-获取MySQL数据的主要内容,如果未能解决你的问题,请参考以下文章
在android studio中升级repo v9后,片段必须是公共静态类崩溃错误
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段