当前位置: 首页>>代码示例>>Python>>正文


Python Session.load_from_db方法代码示例

本文整理汇总了Python中session.Session.load_from_db方法的典型用法代码示例。如果您正苦于以下问题:Python Session.load_from_db方法的具体用法?Python Session.load_from_db怎么用?Python Session.load_from_db使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在session.Session的用法示例。


在下文中一共展示了Session.load_from_db方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_from_db

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import load_from_db [as 别名]
 def load_from_db(self, connection, user_id):
     sql_data = {
         'user_id': user_id
     }
     sql_query = 'SELECT id, nickname, login, password, type FROM User WHERE id = %(user_id)s LIMIT 1'
     with connection:
         cur = connection.cursor()
         cur.execute(sql_query, sql_data)
         loaded_data = cur.fetchall()[0]
         self.user_id = loaded_data[0]
         self.nickname = loaded_data[1]
         self.login = loaded_data[2]
         self.password = loaded_data[3]
         if loaded_data[4] == 0:
             self.__class__ = User
         elif loaded_data[4] == 1:
             self.__class__ = Moderator
         else:
             self.__class__ = Administrator
         sql_query = 'SELECT id FROM Session WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Session()
             loaded_object.load_from_db(connection, x)
             self.sessions.append(loaded_object)
         sql_query = 'SELECT id FROM Currency WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Currency()
             loaded_object.load_from_db(connection, x)
             self.currencies[loaded_object.name] = loaded_object
         sql_query = 'SELECT id FROM Achievment WHERE user_id=%(user_id)s'
         cur.execute(sql_query, sql_data)
         id_data = [x[0] for x in cur.fetchall()]
         for x in id_data:
             loaded_object = Achievement()
             loaded_object.load_from_db(connection, x)
             self.achievements.append(loaded_object)
         sql_query = 'SELECT id FROM Counter WHERE user_id=%(user_id)s LIMIT 1'
         cur.execute(sql_query, sql_data)
         id_data = cur.fetchall()[0][0]
         self.counters = Counter()
         self.counters.load_from_db(connection, id_data)
开发者ID:po1nt710,项目名称:homework,代码行数:47,代码来源:user.py

示例2: Player

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import load_from_db [as 别名]
class Player(object):

    def __init__(self, name=None, money=None, score=None, level=None):
        self.name = name
        self.money = money
        self.level = level
        self.score = score
        self.session = Session()

    def as_dict(self):
        d = {}
        d["score"] = self.score
        d["level"] = self.level
        d["session"] = self.session.as_dict()
        d["type"] = self.__class__.__name__
        d["name"] = self.name
        d["money"] = self.money
        return d

    def save(self, file_object):
        json.dump(self.as_dict(),file_object)

    def load(self, file_object):
        object_as_dict = json.load(file_object)
        self.level = object_as_dict["level"]
        self.lastSession = Session(object_as_dict['session']['starttime'], object_as_dict['session']['stoptime'])
        self.name = object_as_dict["name"]
        self.money = object_as_dict["money"]
        return object_as_dict

    def load_from_db(self, id):
        self.session.load_from_db(player_id=id)
        sql_data = {
            'id': id,
        }
        sql_query = 'SELECT name, score, level, money FROM player WHERE id=%(id)s'
        with connection:
            cur = connection.cursor()
            cur.execute(sql_query, sql_data)
            for row in cur.fetchall():
                if row:
                    print "row is {}".format(row)
                    self.name = row[0]
                    self.score = int(row[1])
                    self.level = int(row[2])
                    self.money = int(row[3])
                else:
                    raise MySQLdb.IntegrityError("Record not found. Check input 'id'=={} parameter!".format(id))

        return (self.name, self.score, self.level, self.money)

    def delete_from_db(self, id):
        self.session.delete_from_db(id)
        sql_data = {
            'id': id,
        }
        sql_query = 'DELETE FROM player WHERE id=%(id)s'
        with connection:
            cur = connection.cursor()
            cur.execute(sql_query, sql_data)
            print("Player with id={} has been deleted".format(id))

        return True

    def save_to_db(self, id):
        sql_data = {
            'id': id,
            'name': self.name,
            'score': self.score,
            'level': self.level,
            'money': self.money,
        }
        sql_query = 'INSERT INTO player (id, name, score, level, money) VALUES (%(id)s, %(name)s, %(score)s, %(level)s, %(money)s)'
        with connection:
            cur = connection.cursor()
            try:
                cur.execute(sql_query, sql_data)
                print "New record was inserted"
            except MySQLdb.IntegrityError:
                sql_query = 'UPDATE player SET name=%(name)s, score=%(score)s, level=%(level)s, money=%(money)s WHERE id=%(id)s'
                cur.execute(sql_query, sql_data)
                print "Record was updated!"

        self.session.save_to_db(player_id=id)

        return True

    def login(self):
        self.session.startSession()
        print 'Login successfull'
        return self.session.starttime

    def logout(self):
        self.session.stopSession()
        print 'Logout successfull'
        return self.session.stoptime

    def __str__(self):
         return '{}(name="{}", money="{}", score="{}", level="{}")'.format(self.__class__.__name__, self.name, self.money, self.score, self.level) + self.session.__str__()
开发者ID:freemansgithub,项目名称:PythonLessons,代码行数:101,代码来源:player.py


注:本文中的session.Session.load_from_db方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。