โค้ด: เลือกทั้งหมด
def neworderform():
while True:
o = input('หมายเลขใบสั่งซื้อสินค้า:')
if o == 'exit' or o == '':
break
t = input('วันที่')
cf = input('ชื่อลูกค้า')
cl = input('นามสกุลลูกค้า')
ef = input('ชื่อพนักงาน')
el = input('นามสกุลพนักงาน')
#-------------------------------------ค้นหารหัสลูกค้าจากชื่อที่อยู่ในตัวแปร cf และ cl ------------------------------
sql = "select * from customer where firstname='"
cur.execute(sql + cf + "'and lastname ='" + cl + "'")
x = cur.fetchall()
c = x[0][0] #อ่านรหัสลูกค้า แล้วเก็บไว้ในตัวแปร c
#---------------------------------------ค้นหารหัสลูกค้าจากชื่อที่อยู่ในตัวแปร ef และ el-----------------------------
sql = "select * from employee where firstname='"
cur.execute(sql + ef + "'and last name ='" + el + "'")
x = cur.fetchall()
e = x[0][0]#อ่านรหัสลูกค้าเก็บไว้ในตัวแปร e
#---------------------------------------จัดเก็บข้อมูลใบสั่งซื้อส่วนแรกสุดในตาราง orderproduct ---------------------
s = "insert into orderproduct values(%s,'%s','%s','%s')"
cur.execute(s % (o, t, c, e))
#----------------------------------------ป้อนรายการสินค้าที่ซื้อและจัดเก็บลงในตาราง orderdetail----------------------
print("ป้อนรายการสินค้าที่ซื้อ")
while True:
num = input('ลำดับที่ :')
if num == 'exit' or num == '':
break
p = input('สินค้า:')
qty = input('จำนวน:')
cost = input('ราคา:')
##------------------------------------ค้นหารหัสสินค้าในตัวแปร p -----------------------------------------
cur.execute("select * from product where title='" + p + "'")
x = cur.fetchall()
pc = x[0][0] ######อ่านรหัสลูกค้าแล้วนำมาเก็บไว้ในตัวแปร e
#-------------------------------จัดเก็บข้อมูลใบสั่งซื้อส่วนที่สองในตาราง orderdetail ---------------------------
s = "insert into orderdetail values(%s',%s',%s',%s',%s')"
cur.execute(s % (num + o, o, pc, qty, cost))
cn.commit()
เมนูการเพิ่มข้อมูล
1.ป้อนรายละเอียดเกี่ยวกับสินค้า
2.ป้อนข้อมูลลูกค้า
3.ป้อนข้อมูลพนักงาน
4.ป้อนข้อมูลใบสั่งซื้อสินค้า
5.กดปุ่ม Enter สำหรับกลับเมนูหลัก
เลือกรายการที่ต้องการ:'4'----------#กรอก 4
หมายเลขใบสั่งซื้อสินค้า:'1000----------#กรอก 1000
วันที่'1/1/2011'----------------------#กรอก 1/1/2011
ชื่อลูกค้า'Dan'------------------------#กรอก Dan
นามสกุลลูกค้า'Kendy'----------------#กรอก Kendy
ชื่อพนักงาน'Kim'---------------------#กรอก Kim
นามสกุลพนักงาน'Young'------------# เมื่อกรอก Young แล้วกด Enter เพื่อที่จะ add ข้อมูลก็จะเกิด Error ดังนี้ครับ
โค้ด: เลือกทั้งหมด
Traceback (most recent call last):
File "C:\Pcode\test-firstprogram.py", line 393, in <module>
menuinput()
File "C:\Pcode\test-firstprogram.py", line 186, in menuinput
neworderform()
File "C:\Pcode\test-firstprogram.py", line 144, in neworderform
cur.execute(sql + ef + "'and last name ='" + el + "'")
File "c:\users\benz\appdata\local\temp\easy_install-do7vxk\MySQL_python-1.2.4c1-py2.7-win32.egg.tmp\MySQLdb\cursors.py", line 201, in execute
self.errorhandler(self, exc, value)
File "c:\users\benz\appdata\local\temp\easy_install-do7vxk\MySQL_python-1.2.4c1-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name ='Young'' at line 1")