Beispiel #1
0
 def delCart(username, goodsId, goodsNum) -> bool:
     conn = store.get_db_conn()
     try:
         cursor = conn.execute(
             "SELECT goodsNum, goodsPrice from cart where username=? and goodsId=?",
             (username, goodsId),
         )
         row = cursor.fetchone()
         if row is None:
             return False
         newgoodsNum = row[0] - goodsNum
         newtotalValue = row[1] * newgoodsNum
         if newgoodsNum == 0:
             cursor = conn.execute(
                 "DELETE from cart where username=? and goodsId=?",
                 (username, goodsId))
         else:
             conn.execute(
                 "UPDATE cart set goodsNum = ? where username=? and goodsId=?",
                 (newgoodsNum, username, goodsId),
             )
             conn.execute(
                 "UPDATE cart set totalValue = ? where username=? and goodsId=?",
                 (newtotalValue, username, goodsId),
             )
         conn.commit()
     except BaseException as e:
         print(e)
         conn.rollback()
         return False
     return True
Beispiel #2
0
 def update_token(self):
     conn = store.get_db_conn()
     try:
         conn.execute(
             "UPDATE user set token = ?, terminal = ? where username=?",
             (self.token, self.terminal, self.username),
         )
         conn.commit()
     except sqlite.Error as e:
         logging.error(str(e))
         conn.rollback()
Beispiel #3
0
 def delete_user(self):
     conn = store.get_db_conn()
     try:
         cursor = conn.execute("DELETE from user where username=?", (self.username,))
         if cursor.rowcount == 1:
             conn.commit()
         else:
             conn.rollback()
     except sqlite.Error as e:
         logging.error(str(e))
         conn.rollback()
Beispiel #4
0
 def cancelOrder(orderId):
     conn = store.get_db_conn()
     try:
         cursor = conn.execute("DELETE from order where username=?",
                               (orderId))
         if cursor.rowcount == 1:
             conn.commit()
         else:
             conn.rollback()
     except sqlite.Error as e:
         logging.error(str(e))
         conn.rollback()
Beispiel #5
0
 def addGoods(goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr, sellerName) -> bool:
     conn = store.get_db_conn()
     try:
         conn.execute(
             "INSERT into goods(goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr, sellerName) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
             (goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr, sellerName),
         )
         conn.commit()
     except BaseException as e:
         print(e)
         conn.rollback()
         return False
     return True
Beispiel #6
0
def insert_user(username, password) -> bool:
    conn = store.get_db_conn()
    try:
        conn.execute(
            "INSERT into user(username, password, token, terminal) VALUES (?, ?, '', '');",
            (username, password),
        )
        conn.commit()
    except sqlite.Error as e:
        print(e)
        conn.rollback()
        return False
    return True
Beispiel #7
0
def check_num(goodsId, goodsNum):
    try:
        conn = store.get_db_conn()
        cursor = conn.execute(
            "SELECT goodsNum from goods where goodsId=?",
            (goodsId),
        )
        row = cursor.fetchone()
        if row is None or row[1] - goodsNum < 0:
            return False
    except sqlite.Error as e:
        logging.error(str(e))
        return False
    return True
Beispiel #8
0
 def createOrder(orderId, sellerName, buyerName, orderStatus, cartlist,
                 addr):
     conn = store.get_db_conn()
     try:
         for good in cartlist:
             conn.execute(
                 "INSERT into orders(orderId, buyerName, sellerName , orderStatus, goodsName, goodsPrice, totalValue, addr) VALUES (?, ?, ?, ?, ?, ?, ? ,?);",
                 (orderId, buyerName, sellerName, orderStatus, good[0],
                  good[1], good[2], addr),
             )
             conn.commit()
     except sqlite.Error as e:
         print(e)
         conn.rollback()
         return False
     return True
Beispiel #9
0
    def searchGoods(keywords, goodsType) -> (bool, list):
            conn = store.get_db_conn()
            try:
                if keywords == "" and goodsType == "":
                    return False, []

                elif keywords != "" and goodsType == "":
                    cursor = conn.execute(
                        "SELECT goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr,sellerName from goods where goodsname=?",
                        (keywords,),
                    )
                    contents = cursor.fetchall()
                    if not contents:
                        cursor = conn.execute(
                            "SELECT goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr,sellerName from goods where goodsDsr LIKE ?",
                            ('%' + keywords + '%',),
                        )
                        contents = cursor.fetchall()
                        if not contents:
                            return False, []

                elif keywords == "" and goodsType != "":
                    cursor = conn.execute(
                        "SELECT goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr,sellerName from goods where goodsType=?",
                        (goodsType,),
                    )
                    contents = cursor.fetchall()
                    if not contents:
                        return False, []
                else:
                    cursor = conn.execute(
                        "SELECT goodsId, goodsName, goodsAuth, goodsPrice, goodsNum, goodsType, goodsDsr,sellerName from goods where goodsType=? and goodsname=?",
                        (goodsType, keywords),
                    )
                    contents = cursor.fetchall()
                    if not contents:
                        return False, []

                goodslist = []
                for row in contents:
                    a = [row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]]
                    goodslist.append(a)

            except sqlite.Error as e:
                logging.error(str(e))
                return False, []
            return True, goodslist
Beispiel #10
0
 def fetch_user(self, username) -> bool:
     try:
         conn = store.get_db_conn()
         cursor = conn.execute(
             "SELECT username, password, token, terminal from user where username=?",
             (username,),
         )
         row = cursor.fetchone()
         if row is None:
             return False
         self.username = username
         self.password = row[1]
         self.token = row[2]
         self.terminal = row[3]
     except sqlite.Error as e:
         logging.error(str(e))
         return False
     return True
Beispiel #11
0
 def addCart(username, goodsId, goodsName, goodsPrice, goodsNum,
             totalValue) -> bool:
     conn = store.get_db_conn()
     try:
         #添加一个判断,商品是否还有库存
         if not check_num(goodsId, goodsNum):
             return False
         conn.execute(
             "INSERT into cart(username, goodsId, goodsName, goodsPrice, goodsNum, totalValue) VALUES (?, ?, ?, ?, ?, ?);",
             (username, goodsId, goodsName, goodsPrice, goodsNum,
              totalValue),
         )
         conn.commit()
     except BaseException as e:
         print(e)
         conn.rollback()
         return False
     return True
Beispiel #12
0
    def fetch_order(username) -> (bool, list):
        conn = store.get_db_conn()
        try:

            cursor = conn.execute(
                "SELECT orderId, orderStatus, goodsName, goodsPrice, totalValue, addr from order where username=?",
                (username),
            )
            contents = cursor.fetchall()
            if contents is None:
                return False, []
            orderlist = []
            for row in contents:
                a = [row[0], row[1], row[2], row[3], row[4], row[5]]
                orderlist.append(a)
        except sqlite.Error as e:
            logging.error(str(e))
            return False
        return True, orderlist
Beispiel #13
0
 def getCart(username) -> (bool, list, float):
     conn = store.get_db_conn()
     try:
         cursor = conn.execute(
             "SELECT goodsName, goodsPrice, goodsNum, totalValue from cart where username=?",
             (username),
         )
         contents = cursor.fetchall()
         if contents is None:
             return False, []
         cartlist = []
         sum = 0
         for row in contents:
             a = [row[0], row[1], row[2]]
             sum += row[3]
             cartlist.append(a)
     except sqlite.Error as e:
         logging.error(str(e))
         return False
     return True, cartlist, sum
Beispiel #14
0
 def __init__(self):
     self.conn = store.get_db_conn()
Beispiel #15
0
 def __init__(self):
     self.conn = store.get_db_conn()
     self.cursor = self.conn.cursor()