def get_friends(self, friends): connection = pymysql.connect(host=db.db_host, port=db.db_port, user=db.db_user, passwd=db.db_passwd, db=db.db_name) cursor = connection.cursor() friends = json.loads(friends) my_friends = list() for friend in friends["Friends"]: friend["PHONE"] = friend["PHONE"].replace(' ', '').replace('+34', '') phone = friend["PHONE"].replace(' ', '').replace('(', '').replace( ')', '').replace('-', '') if phone == '': phone = 0 SQL = """SELECT COUNT(*) FROM USERS WHERE PHONE = %s""".replace('\n', ' ').replace('\t', '') SQL %= str(phone) SQL = SQL.replace('\u202c', '').replace('\u202a', '') try: cursor.execute(SQL) except: return SQL count = cursor._rows[0][0] if count > 0: SQL = """SELECT * FROM USERS WHERE PHONE = %s""".replace('\n', ' ').replace('\t', '') SQL %= str(phone) SQL = SQL.replace('\u202c', '').replace('\u202a', '') cursor.execute(SQL) user = User() user.ID = cursor._rows[0][0] user.NICK = cursor._rows[0][1] user.STATUS = cursor._rows[0][2] user.PHONE = cursor._rows[0][3] user.USER_IMAGE = cursor._rows[0][4] my_friends.append({'friend': user.serialize()}) cursor.close() connection.close() return my_friends
def get_friends(self, friends): connection = pymysql.connect(host=db.db_host, port=db.db_port, user=db.db_user, passwd=db.db_passwd, db=db.db_name) cursor = connection.cursor() friends = json.loads(friends) my_friends = list() for friend in friends["Friends"]: friend["PHONE"] = friend["PHONE"].replace(' ', '').replace('+34', '') phone = friend["PHONE"].replace(' ', '').replace('(', '').replace(')', '').replace('-', '') if phone == '': phone = 0 SQL = """SELECT COUNT(*) FROM USERS WHERE PHONE = %s""".replace('\n', ' ').replace('\t', '') SQL %= str(phone) SQL = SQL.replace('\u202c', '').replace('\u202a', '') try: cursor.execute(SQL) except: return SQL count = cursor._rows[0][0] if count > 0: SQL = """SELECT * FROM USERS WHERE PHONE = %s""".replace('\n', ' ').replace('\t', '') SQL %= str(phone) SQL = SQL.replace('\u202c', '').replace('\u202a', '') cursor.execute(SQL) user = User() user.ID = cursor._rows[0][0] user.NICK = cursor._rows[0][1] user.STATUS = cursor._rows[0][2] user.PHONE = cursor._rows[0][3] user.USER_IMAGE = cursor._rows[0][4] my_friends.append({'friend': user.serialize()}) cursor.close() connection.close() return my_friends
def get_user(self, ID): connection = pymysql.connect(host=db.db_host, port=db.db_port, user=db.db_user, passwd=db.db_passwd, db=db.db_name) cursor = connection.cursor() SQL = "SELECT * FROM USERS WHERE ID_USER = %s OR PHONE = %s;" SQL = SQL % (str(ID), str(ID)) cursor.execute(SQL) user = User() user.ID = cursor._rows[0][0] user.NICK = cursor._rows[0][1] user.STATUS = cursor._rows[0][2] user.PHONE = cursor._rows[0][3] user.USER_IMAGE = cursor._rows[0][4] user.LAST_RECEIVED_MESSAGE = cursor._rows[0][5] cursor.close() connection.close() return user.serialize()
def get_users(self): connection = pymysql.connect(host=db.db_host, port=db.db_port, user=db.db_user, passwd=db.db_passwd, db=db.db_name) cursor = connection.cursor() cursor.execute("SELECT * FROM USERS") users = list() for row in cursor: user = User() user.ID = row[0] user.NICK = row[1] user.STATUS = row[2] user.PHONE = row[3] user.USER_IMAGE = row[4] user.LAST_RECEIVED_MESSAGE = row[5] users.append(user.serialize()) cursor.close() connection.close() return users