Python代码两次插入同一行

时间:2018-03-15 20:40:25

标签: python sql-server pyodbc

我的方法继续重复并在我的表中插入两次。我使用的是pyqt5和mssql服务器管理工​​作室。我没有任何迹象表明它重复,但我也没有看到问题。

self.name = self.NameLine.text()
self.loc = self.LocLine.text()
self.cn = self.CNLine.text()
self.ct = self.CTLine.text()
self.pm = self.PMBox.currentText()
self.dm = self.DMBox.currentText()
self.item = self.ItemLine.text()
self.unit = self.UnitLine.text()
self.ID = self.IDLine.text()

unit = int(self.unit)
print('1')
date = QDate.currentDate().toString()

conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=GRILL\SQLEXPRESS;"
                      "Database=FinalSuede;"
                      "Trusted_Connection=yes;")
cursor = conn.cursor()

sql = "INSERT INTO Orders(Name, Address, ContactNumber, ContactThrough, PaymentMethod, DeliveryMethod," \
      "ItemName, Units, TransactionID, Date, Status) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(self.name,
                                                                                                                             self.loc,
                                                                                                                             self.cn,
                                                                                                                             self.ct,
                                                                                                                             self.pm,
                                                                                                                             self.dm,
                                                                                                                             self.item,
                                                                                                                             unit,
                                                                                                                             self.ID,
                                                                                                                             date,
                                                                                                                             "Pending")

cursor.execute(sql)

unitsold = "UPDATE Items SET UnitsSold = UnitsSold + '{}' WHERE ItemName = '{}'".format(unit, self.item)
unitleft = "UPDATE Items SET UnitsLeft = UnitsLeft - '{}' WHERE ItemName = '{}'".format(unit, self.item)

cursor.execute(unitsold)
cursor.execute(unitleft)

conn.commit()

0 个答案:

没有答案