尝试在mysql中插入datetime时出现此错误。
mysql列inv_date数据类型是timestamp(6),而我正在使用QtCore.QDateTime.currentDateTime()从我的计算机中检索当前日期和时间。 pyqt5中的格式和代码可能是什么?
提前谢谢!
这里是完整的代码:
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot
import MySQLdb as mdb
import datetime
import math
currentDT = datetime.datetime.now
from A3AMainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.btn_inv_save.setEnabled(False)
self.btn_inv_cancel.setEnabled(False)
self.dt_inv_datetime.setDateTime(QtCore.QDateTime.currentDateTime())
self.de_inv_expiry.setDate(QtCore.QDate.currentDate())
self.de_inv_duedate.setDate(QtCore.QDate.currentDate())
self.initWindow()
def initWindow(self):
self.le_inv_invnum.textChanged.connect(self.newText)
self.le_inv_supplier.textChanged.connect(self.newText)
self.le_inv_batch.textChanged.connect(self.newText)
self.le_inv_quantity.textChanged.connect(self.newText)
self.le_inv_listprice.textChanged.connect(self.newText)
self.le_inv_discount.textChanged.connect(self.newText)
self.le_inv_netprice.textChanged.connect(self.newText)
self.le_inv_totalamount.textChanged.connect(self.newText)
self.btn_inv_save.clicked.connect(self.click_save)
self.btn_inv_cancel.clicked.connect(self.Cancel)
def Cancel(self):
self.dt_inv_datetime.setDateTime(QtCore.QDateTime.currentDateTime())
self.le_inv_invnum.setText('')
self.le_inv_supplier.setText('')
self.cb_inv_prodname.setItemText(0,'Eprex 4kiu')
self.te_inv_itemdesc.setText('')
self.le_inv_batch.setText('')
self.de_inv_expiry.setDate(QtCore.QDate.currentDate())
self.le_inv_quantity.setText('')
self.le_inv_listprice.setText('')
self.le_inv_discount.setText('')
self.le_inv_netprice.setText('')
self.le_inv_totalamount.setText('')
self.cb_inv_terms.setItemText(0,'COD')
self.de_inv_duedate.setDate(QtCore.QDate.currentDate())
self.cb_inv_status.setItemText(0,'Fully Paid')
def newText(self):
if self.le_inv_invnum.text() and self.le_inv_supplier.text() and self.le_inv_batch.text() and self.le_inv_quantity.text() and self.le_inv_listprice.text() and self.le_inv_discount.text() and self.le_inv_netprice.text() and self.le_inv_totalamount.text():
self.btn_inv_save.setEnabled(True)
self.btn_inv_cancel.setEnabled(True)
self.btn_inv_edit.setEnabled(False)
self.btn_inv_prev.setEnabled(False)
self.btn_inv_next.setEnabled(False)
self.btn_inv_search.setEnabled(False)
else:
self.btn_inv_save.setEnabled(False)
self.btn_inv_cancel.setEnabled(False)
self.btn_inv_edit.setEnabled(True)
self.btn_inv_prev.setEnabled(True)
self.btn_inv_next.setEnabled(True)
self.btn_inv_search.setEnabled(True)
@pyqtSlot()
def click_save(self):
con = mdb.connect(user='root', passwd='ilovemy1god',
host='localhost', database='A3A_SIS')
with con:
cur = con.cursor()
cur.execute('''INSERT INTO inv_delivery(inv_date, inv_invoice_num, inv_sup_name, inv_prod_name, inv_prod_itemdesc, inv_batchnum, inv_expiry, inv_quantity, inv_list_price, inv_discount, inv_net_price, inv_total_amount, inv_pay_terms, inv_due_date, inv_status)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''',
( self.dt_inv_datetime.dateTime(),
self.le_inv_invnum.text(),
self.le_inv_supplier.text(),
self.cb_inv_prodname.currentIndex(),
self.te_inv_itemdesc.document(),
self.le_inv_batch.text(),
self.de_inv_expiry.date(),
self.le_inv_quantity.text(),
self.le_inv_listprice.text(),
self.le_inv_discount.text(),
self.le_inv_netprice.text(),
self.le_inv_totalamount.text(),
self.cb_inv_terms.currentIndex(),
self.de_inv_duedate.date(),
self.cb_inv_status.currentIndex()
)
)
cur.close()
QMessageBox.information(self, "Connection", "Data Inserted Successfully")
self.dt_inv_datetime.setDateTime(QtCore.QDateTime.currentDateTime())
self.le_inv_invnum.setText('')
self.le_inv_supplier.setText('')
self.cb_inv_prodname.setItemText(0,'Eprex 4kiu')
self.te_inv_itemdesc.setText('')
self.le_inv_batch.setText('')
self.de_inv_expiry.setDate(QtCore.QDate.currentDate())
self.le_inv_quantity.setText('')
self.le_inv_listprice.setText('')
self.le_inv_discount.setText('')
self.le_inv_netprice.setText('')
self.le_inv_totalamount.setText('')
self.cb_inv_terms.setItemText(0,'COD')
self.de_inv_duedate.setDate(QtCore.QDate.currentDate())
self.cb_inv_status.setItemText(0,'Fully Paid')
self.initWindow()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = MainWindow()
#ui = Ui_MainWindow()
#ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
尝试通过打印PyQt5.QtCore.QDateTime(2018, 5, 24, 18, 53, 17, 705)
的值进行调试。
作为一般的走动,你可以尝试发送一个'YYYY-MM-DD HH:MM:SS`形式的字符串(24小时内的小时)。许多DBMS会接受从此字符串到二进制日期时间的自动转换。