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


Python Session.save_to_db方法代码示例

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


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

示例1: Player

# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import save_to_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.save_to_db方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。