async def get_blogs_by_page(db, page): blogs = list() if page == None: page = 0 else: page = int(page) sql = """SELECT blog.Id, blog.uId, blog.title, blog.contents, blog.utime, blog.mtime, author.nickname FROM blog,author WHERE blog.uId=author.Id ORDER BY blog.Id DESC LIMIT {},10""".format( 10 * page) logger.debug(sql) try: async with db.cursor() as cur: await cur.execute(sql) rets = await cur.fetchall() if len(rets) > 0: for r in rets: blogs.append( dbmodel.Blog( int(r[0]), int(r[1]), str(r[2]), str(r[3]), r[4], r[5], str(r[6]), )) # cur.close() except Exception as e: logger.error(e) finally: pass return blogs
async def get_blog_by_id(db, id): blog = None try: async with db.cursor() as cur: if int(id) > 0: await cur.execute( """SELECT blog.Id, blog.uId, blog.title, blog.contents, author.nickname FROM blog, author WHERE blog.uId=author.Id AND blog.Id=%s""", (int(id)), ) ret = await cur.fetchone() if len(ret) > 0: blog = dbmodel.Blog( int(ret[0]), int(ret[1]), str(ret[2]), str(ret[3]), 0, 0, str(ret[4]), ) # await cur.close() except Exception as e: logger.error(e) finally: pass return blog
async def get_blog_by_id(db, id): blog = None cur = db.cursor() try: if int(id) > 0: cur.execute( '''SELECT blog.Id, blog.uId, blog.title, blog.contents, author.nickname FROM blog, author WHERE blog.uId=author.Id AND blog.Id=?''', (int(id), )) ret = cur.fetchone() if ret and len(ret) > 0: blog = dbmodel.Blog(int(ret[0]), int(ret[1]), str(ret[2]), str(ret[3]), str(ret[4])) except Exception as e: logger.error(e) return blog
async def get_answer_best_top(db, counts=10): blogs = list() sql = """SELECT blog.Id, blog.uId, blog.title, blog.utime, blog.mtime, author.nickname FROM blog,author WHERE blog.uId=author.Id and blog.utime > 0 ORDER BY blog.utime DESC LIMIT {}""".format( counts) logger.debug(sql) try: async with db.cursor() as cur: await cur.execute(sql) rets = await cur.fetchall() if len(rets) > 0: for r in rets: blogs.append( dbmodel.Blog(int(r[0]), int(r[1]), str(r[2]), "", r[3], r[4], str(r[5]))) # cur.close() except Exception as e: logger.error(e) finally: pass return blogs
async def get_blogs_by_page(db, page): blogs = list() if page == None: page = 0 else: page = int(page) sql = '''SELECT blog.Id, blog.uId, blog.title, blog.contents, author.nickname FROM blog,author WHERE blog.uId=author.Id ORDER BY blog.Id DESC LIMIT {},10'''.format( 10 * page) logger.debug(sql) cur = db.cursor() try: cur.execute(sql) rets = cur.fetchall() if len(rets) > 0: for r in rets: blogs.append( dbmodel.Blog(int(r[0]), int(r[1]), str(r[2]), str(r[3]), str(r[4]))) except Exception as e: logger.error(e.args) finally: return blogs