A simple shopping process written in Python (including an operational database)

Source: Internet
Author: User

#!/usr/bin/python

Import MySQLdb


def select (table,value,item,res):

Try

Conn=mysqldb.connect (host= ' localhost ', user= ' root ', db= ' shop ', port=3306)

Cur=conn.cursor ()

Sql= "Select" +value+ "from" +table+ "where" +item+ "=" + "%s"

Cur.execute (Sql,res)

rows = Cur.fetchmany (1)

return rows

Cur.close ()

Conn.close ()

Except Mysqldb.error,e:

Print "Mysql Error%d:%s"% (E.args[0], e.args[1])


def update (Username,money):

Try

Conn=mysqldb.connect (host= ' localhost ', user= ' root ', db= ' shop ', port=3306)

Cur=conn.cursor ()

sql= "Update user set count=%s where username= '%s '"% (money,username)

Cur.execute (SQL)

rows = Cur.fetchmany (1)

return rows

Conn.commit ()

Cur.close ()

Conn.close ()

Except Mysqldb.error,e:

Print "Mysql Error%d:%s"% (E.args[0], e.args[1])



def buy ():

Name=raw_input ("Please input your name")

COUNT=STR (SELECT (' User ', ' count ', ' username ', name) [0]). Split (' L ') [0].split (' (') [1]

If Count:

Item=raw_input ("Please input your want to buy things")

Num=int (raw_input ("Please input the number of things you want to buy")

Price=int (str (SELECT (' Item ', ' Price ', ' item ', item) [0]). Split (' L ') [0].split (' (') [1])

If Count > Price*num:

S_money=int (count)-price*num

Update (Name,s_money)

Print "Your account balance is", S_money

Else

Print "Your account balance was not enough"


Buy ()



This article is from the "9400142" blog, please be sure to keep this source http://9410142.blog.51cto.com/9400142/1691749

A simple shopping process written in Python (including an operational database)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.