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


Python Mysql.fetchone方法代码示例

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


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

示例1: __init__

# 需要导入模块: from mysql import Mysql [as 别名]
# 或者: from mysql.Mysql import fetchone [as 别名]
class Subscribe:
    def __init__(self):
        db_config = config.getConfig('db')
        weibo_config = config.getConfig('weibo')
        app_config = config.getConfig('app')
        
        self.db = Mysql(db_config['host'], db_config['user'], db_config['passwd'], db_config['db'], db_config['charset'])
        self.user = User()
        self.wb = Weibo(weibo_config['username'], weibo_config['password'], weibo_config['appkey'])
        self.config = app_config

    def add(self, gtalk, url):
        print 'add %s %s' % (gtalk, url)
        
        if not self.user.checkUrl(url):
            return '微博URL格式错误!'
        
        if url == 'http://weibo.com/u/2268327783':
            return '您不能订阅我哦~'
        
        # 查询已订阅条数
        sql = "SELECT COUNT(uid) cnt FROM subs WHERE gtalk='%s'" % gtalk
        count = self.db.fetchone(sql)
        if count and int(count) >= int(self.config['sub_limit']):
            return '您最多只能订阅%d条哦!' % int(self.config['sub_limit'])
        
        # 查询是否已订阅
        sql = "SELECT COUNT(uid) cnt FROM subs WHERE gtalk='%s' AND url='%s'" % (gtalk, url)
        count = self.db.fetchone(sql)
        if count and int(count) > 0:
            return '您已经订阅:%s!' % url
        
        # 查询url-uid对应表
        (uid, nick) = self.user.getUidfromurl(self.db, url)
            
        if uid == -1:
            return '参数格式错误!'
        if uid == 0:
            return '账号不存在!'
        
        # 查询是否已关注
        sql = "SELECT count FROM follows WHERE uid=%d" % uid
        count = self.db.fetchone(sql)
        
        if count and int(count) > 0:
            # 关注表+1
            sql = "UPDATE follows SET count=count+1 WHERE uid=%d" % uid
        else:
            # 关注用户
            self.wb.Follow(uid)
            
            # 插入关注表
            sql = "INSERT INTO follows (uid, count) VALUES (%d, 1)" % uid
            
        self.db.query(sql)
        
        # 插入订阅表
        sql = "INSERT INTO subs (gtalk, url, uid, time) VALUES ('%s', '%s', %d, %d)" % (gtalk, url, uid, int(time.time()))
        self.db.query(sql)
        
        # 更新用户表
        sql = "REPLACE INTO users (uid, nick) VALUES (%d, '%s')" % (uid, nick)
        self.db.query(sql)
        
        return '%s 订阅成功!' % nick
    
    def delete(self, gtalk, url):
        print 'delete %s %s' % (gtalk, url)
        
        if not self.user.checkUrl(url):
            return '微博URL格式错误!'        
        
        # 查询是否已订阅
        sql = "SELECT COUNT(uid) cnt FROM subs WHERE gtalk='%s' AND url='%s'" % (gtalk, url)
        count = self.db.fetchone(sql)
        if count == None or int(count) == 0:
            return '您还未订阅:%s!' % url
        
        # 查询url-uid对应表
        (uid, nick) = self.user.getUidfromurl(self.db, url)
            
        if uid == -1:
            return '参数格式错误!'
        if uid == 0:
            return '账号不存在!'
        
        # 查询是否已关注
        sql = "SELECT count FROM follows WHERE uid=%d" % uid
        count = self.db.fetchone(sql)
        if count:
            if int(count) <= 1:
                # 删除关注表记录
                sql = "DELETE FROM follows WHERE uid=%d" % uid
                
                # 取消用户
                self.wb.Unfollow(uid)
            else:
                # 关注表-1
                sql = "UPDATE follows SET count=count-1 WHERE uid=%d" % uid
                
#.........这里部分代码省略.........
开发者ID:squarezhou,项目名称:weibo-notify,代码行数:103,代码来源:subscribe.py


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