Beispiel #1
0
 def conn(self):
     try:
         self.client = pymongo.MongoClient(host=self.host, port=self.port)
         self.db = self.client[self.db]
         self.collection = self.db[self.collection]
         print('\n', public_tools.get_time(), "mongo连接成功")
     except BaseException as e:
         print('\n', public_tools.get_time(), 'mongo连接失败:', e.args[0],
               e.args[1])
Beispiel #2
0
 def conn(self):
     try:
         self.client = redis.Redis(host=self.host,
                                   port=self.port,
                                   password=self.password,
                                   decode_responses=True)
         print('\n', public_tools.get_time(), "redis连接成功")
     except BaseException as e:
         print('\n', public_tools.get_time(), 'redis连接失败:', e.args[0],
               e.args[1])
Beispiel #3
0
 def conn(self):
     try:
         self.db = pymysql.Connect(host=self.host,
                                   port=self.port,
                                   user=self.user,
                                   password=self.password,
                                   db=self.db,
                                   charset=self.charsets)
         self.cursor = self.db.cursor()
         print('\n', public_tools.get_time(), 'mysql连接成功')
     except BaseException as e:
         print('\n', public_tools.get_time(), 'mysql连接失败:', e.args[0],
               e.args[1])
Beispiel #4
0
 def get_one(self, sql):
     res = None
     try:
         self.conn()
         self.cursor.execute(sql)
         res = self.cursor.fetchone()
     except BaseException as e:
         print('\n', public_tools.get_time(), '查询失败, error:', e.args[0],
               e.args[1])
     return res
Beispiel #5
0
 def __execute(self, sql):
     count = 0
     try:
         self.conn()
         count = self.cursor.execute(sql)
         self.db.commit()
         self.close()
     except BaseException as e:
         print('\n', public_tools.get_time(), '操作失败, error:', e.args[0],
               e.args[1])
         self.db.rollback()
     return count