Python 아 날로 그 쇼핑 상품 시스템 관련 데이터베이스

4267 단어 학생.
import pymysql

HOST = 'localhost'
PORT = 3306
USERNAME = 'root'
PASSWORD = '123'
DB_NAME = 'jdshop'
CHARSET = 'utf8'
conn = pymysql.Connect(
    # key=value
    host=HOST,
    port=PORT,
    user=USERNAME,
    password=PASSWORD,
    db=DB_NAME,
    charset=CHARSET
)
#         
cursor = conn.cursor()
'''
   shopMessage  ,        
'''


def Sql_M():
    sql = 'select *from shopMessage'
    cursor.execute(sql)  #       
    shop_arr = cursor.fetchall()
    return shop_arr


'''
      shoppingcart(   )  ,        
'''


def Sql_C():  #    
    sql = 'select *from shoppingcart'
    cursor.execute(sql)  #       
    shop_arr = cursor.fetchall()
    return shop_arr


def addShop():
    '''        ,           '''
    shop_arr = Sql_M()
    flag=-1
    num = len(shop_arr) + 1  #       
    name = str(input('       :'))
    count = int(input('       :'))
    for  line in range(0,len(shop_arr)):
        if shop_arr[line][1]==name:
            flag = line#        ,        ,      

    if flag==-1:
       price = int(input('        :'))
       sql = 'INSERT INTO shopMessage(Num,Name,Count,Price) VALUES(%s,"%s",%d,%d)' % (num, name, count, price)
    else:
       count=count+shop_arr[flag][2]
       sql='UPDATE shopMessage SET Count= %d WHERE Name="%s"'% (count,name)
       print(name+"       ...")
    cursor.execute(sql)  #   
    conn.commit()  #     
    print('    ...')

'''
    OK 
    1.sql  ,       : "%s"
    2. values  value
       ?
'''
def showShop():
    #      ,     
    shop_arr = Sql_M()
    print('    \t  \t\t\t  \t   \t')
    for i in range(0, len(shop_arr)):
        print("%s\t\t\t%s\t\t\t%d\t\t%d " % (shop_arr[i][0], shop_arr[i][1], shop_arr[i][2], shop_arr[i][3]))


def searchShop():
    num = str(input('        : '))
    #          
    sql = 'select *from shopMessage WHERE NUM="%s"' % num
    cursor.execute(sql)
    #              ,---    
    shop_arr = cursor.fetchall()
    print('    \t  \t\t\t  \t   \t')
    for i in range(0, len(shop_arr)):
        print("%s\t\t\t%s\t\t\t%d\t\t%d " % (shop_arr[i][0], shop_arr[i][1], shop_arr[i][2], shop_arr[i][3]))
        return shop_arr  #          


def addTocart():
    #          ,     +1
    cart_ar = Sql_C()
    num = len(cart_ar) + 1
    #         
    shop_arr = searchShop()
    flag = -1
    name = shop_arr[0][1]
    count = int(input('       :'))
    price = shop_arr[0][3]
    for  line in range(0,len(cart_ar)):
        if cart_ar[line][1]==name:
            flag = line#        ,        ,      
    if flag==-1:
        sql = 'INSERT INTO shoppingcart(Num,Name,Count,Price) VALUES (%s,"%s",%d,%d)' % (num, name, count, price)
    elif shop_arr[0][2]-count>=0:
       sql='UPDATE shoppingcart SET Count= %d WHERE Name="%s"'% (count+cart_ar[flag][2],name)
    else:
        print("     ...  %s"%shop_arr[0][2]+' ')
        return 0
    sql_shop='UPDATE shopMessage SET Count= %d WHERE Name="%s"'% (shop_arr[0][2]-count,name)#      
    cursor.execute(sql)
    cursor.execute(sql_shop)
    conn.commit()
    print('      ')


def showCart():  #      
    cart_arr = Sql_C()
    print('    \t  \t\t\t  \t   \t')
    for i in range(0, len(cart_arr)):
        print("%s\t\t\t%s\t\t\t%d\t\t%d " % (cart_arr[i][0], cart_arr[i][1], cart_arr[i][2], cart_arr[i][3]))


def print_menu():
    print('1.    ')
    print('2.      ')
    print('3.        ')
    print('4.      ')
    print('5.     ')
    print('6.  ')
    fuction = int(input("          :"))
    return fuction


frag = True
while (frag == True):
    fuction = print_menu()
    if (fuction == 1):
        addShop()  #     
    elif (fuction == 2):
        showShop()
    elif (fuction == 3):
        searchShop()
    elif (fuction == 4):
        addTocart()
    elif (fuction == 5):
        showCart()
    elif (fuction == 6):
        frag = False

좋은 웹페이지 즐겨찾기