# Path: QiuBaiSpider/tools.py
# class Tools(object):
#     def get_current_time(format_="[%Y-%m-%d %H:%M:%S]"):
#         return time.strftime(format_, time.localtime(time.time()))
# 
#     def log(msg=None):
#         print(Tools.get_current_time() + msg)
# 
#     def wash_off_html_tag(str=None):
#         if str is None:
#             return str
#         return re.sub(re.compile(r'<\w+>|</\w+>|<\w+/>|&quot'), '', str)
# 
#     def setup_log_mode(use_file=False, file_name="record.log"):
#         if use_file:
#             sys.stdout = open(file_name, "w")


import pymysql
from QiuBaiSpider.tools import Tools


class DbManager(object):
    sql_create_table = """CREATE TABLE IF NOT EXISTS `QiuShiBaiKe` (
                                `id` int(11) NOT NULL AUTO_INCREMENT,
                                `name` varchar(512) COLLATE utf8_bin NOT NULL,
                                `content` TEXT COLLATE utf8_bin NOT NULL,
                                PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
                            AUTO_INCREMENT=1"""

    def __init__(self):
        self.db = None
        self.cursor = None

    def connect(self):
        try:
            self.db = pymysql.connect("localhost", "yanbober", "XXXXXX", "database_yan_php")
            self.db.set_charset('utf8')
            self.cursor = self.db.cursor()
            self.cursor.execute(DbManager.sql_create_table)
        except Exception as e:
            Tools.log("#ERROR# DbManager connect failed." + str(e))

    def close(self):
        try:
            if self.db is not None:
                self.db.close()
            if self.db is not None:
                self.cursor.close()
        except BaseException as e:

# Path: QiuBaiSpider/tools.py
# class Tools(object):
#     def get_current_time(format_="[%Y-%m-%d %H:%M:%S]"):
#         return time.strftime(format_, time.localtime(time.time()))
# 
#     def log(msg=None):
#         print(Tools.get_current_time() + msg)
# 
#     def wash_off_html_tag(str=None):
#         if str is None:
#             return str
#         return re.sub(re.compile(r'<\w+>|</\w+>|<\w+/>|&quot'), '', str)
# 
#     def setup_log_mode(use_file=False, file_name="record.log"):
#         if use_file:
#             sys.stdout = open(file_name, "w")


# Path: QiuBaiSpider/pymysqldb_manager.py
import pymysql
from QiuBaiSpider.tools import Tools



class DbManager(object):
    sql_create_table = """CREATE TABLE IF NOT EXISTS `QiuShiBaiKe` (
                                `id` int(11) NOT NULL AUTO_INCREMENT,
                                `name` varchar(512) COLLATE utf8_bin NOT NULL,
                                `content` TEXT COLLATE utf8_bin NOT NULL,
                                PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
                            AUTO_INCREMENT=1"""

    def __init__(self):
        self.db = None
        self.cursor = None

    def connect(self):
        try:
            self.db = pymysql.connect("localhost", "yanbober", "XXXXXX", "database_yan_php")
            self.db.set_charset('utf8')
            self.cursor = self.db.cursor()
            self.cursor.execute(DbManager.sql_create_table)
        except Exception as e:
            Tools.log("#ERROR# DbManager connect failed." + str(e))

    def close(self):
        try:
            if self.db is not None:
                self.db.close()
            if self.db is not None:
                self.cursor.close()
        except BaseException as e:

            Tools.log("#ERROR# DbManager close failed."+str(e))