u_mysql.py 2.2 KB
Newer Older
片刻小哥哥's avatar
片刻小哥哥 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#!/usr/bin/python
# coding:utf-8
# -------------------------------------------------------------------------------
# Name: 爬取微博的数据
# Purpose: 找出相近的词
# Author:  jiangzhonglian
# Create_time :  2018年9月14日
# Update_time:   2018年9月14日
# Content:
# Copyright:   (c) jiangzhonglian 2018
# Licence:   <do yourself>
# -------------------------------------------------------------------------------
from pymysql import *


class MySQL:
    def __init__(self,
                 database,
                 host="127.0.0.1",
                 user="xxx",
                 password="xxx",
                 port=3306,
                 charset="utf8"):
        self.host = host
        self.user = user
        self.password = password
        self.port = port
        self.database = database
        self.charset = charset

    # 数据库连接方法:
    def open(self):
        self.db = connect(
            host=self.host,
            user=self.user,
            password=self.password,
            port=self.port,
            database=self.database,
            charset=self.charset)
        # 游标对象
        self.cur = self.db.cursor()

    # 数据库关闭方法:
    def close(self):
        self.cur.close()
        self.db.close()

    # 数据库执行操作方法:
    def execute(self, sql, L=[]):
        try:
            self.open()
            self.cur.execute("%s;" % sql, L)
            self.db.commit()
            print("ok")
            msg = "success"
        except Exception as e:
            # 错误回滚
            self.db.rollback()
            print("Failed", e)
            msg = "fail"
        finally:
            self.close()
        # 返回统一状态
        return msg


    # 数据库查询所有操作方法:
    def execute_all(self, sql, L=[]):
        try:
            self.open()
            self.cur.execute("%s;" % sql, L)
            self.cur.fetchall()
            print("ok")
            msg = "success"
        except Exception as e:
            # 错误回滚
            self.db.rollback()
            print("Failed", e)
            msg = "fail"
        finally:
            self.close()
        # 返回统一状态
        return msg