#!/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)