예제 #1
0
 def library_request(self, data):
     command = bytes.decode(data.command)
     send_data = ""
     if command == "load":
         try:
             con = sqlite3.connect(DATABASE)
             cur = con.cursor()
             query = """ select book_id, title, book_count from book"""
             cur.execute(query)
             while True:
                 row = cur.fetchone()
                 if not row:
                     break
                 send_data = Protocol.BookData(book_id=row[0],
                                               book_title=row[1],
                                               book_cnt=row[2])
                 # if send_data != "":
                 #     send_data += "&"
                 # send_data += row[1]
                 logging.debug(send_data)
                 self.reply(send_data)
                 # self.reply('\n')
             con.close()
         except Exception as e:
             logging.debug(e)
         return send_data
     elif command == "borrow":
         try:
             con = sqlite3.connect(DATABASE)
             book_title = bytes.decode(data.book_title)
             uid = bytes.decode(data.user_id)
             cur = con.cursor()
             query = """insert into borrowbook (user_id, book_id, borrow_dates)
                         select :id, book_id, DATE('now') from book
                         where title = :book_title"""
             query2 = """update book set book_count = book_count-1"""
             cur.execute(query, {'id': uid, 'book_title': book_title})
             cur.execute(query2)
             con.commit()
             con.close()
         except Exception as e:
             logging.debug(e)
         return send_data
     else:
         return send_data