使用pyqt5中的qtcore.qdatetime.currentdatetime()从mysql获取的“error 1292:不正确的datetime值”

bnl4lu3b  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(499)

我在尝试在mysql中插入datetime时遇到了这个错误。
当我使用qtcore.qdatetime.currentdatetime()从我的计算机检索当前日期和时间时,mysql列inv\u date datatype是timestamp(6)。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_())
6rqinv9w

6rqinv9w1#

尝试通过打印 PyQt5.QtCore.QDateTime(2018, 5, 24, 18, 53, 17, 705) .
一般来说,您可以尝试发送格式为“yyyy-mm-dd hh:mm:ss”的字符串(24小时)。许多DBMS会接受从这个字符串到二进制日期时间的自动转换。

相关问题