PyQt5 Qsqltablemodel datatypes "incompatible in the equal to operator" with data over 128 characters

109 Views Asked by At

I'm currently working on a PyQt5 based software that connects to a SQL database for managing assignments for a large number of people. The URLs for these assignment documents are quite long, going up to ~260 characters, and I have been encountering a bug when trying to edit entries with these long links.

QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC SQL Server Driver][SQL Server]The data types nvarchar(max) and ntext are incompatible in the equal to operator. [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared."

The error happens whenever the link is longer than 128 characters, so I'm assuming there is some 16-byte limit somewhere I'm hitting, but I do not know where. I have tried changing the datatypes in the SQL database to different sized nvarchars, nchars, and others. I also tested to make sure it was not doing something strange with links specifically, and used plain text. I have even tried changing it to ntext, and the error will say that

...The data types ntext and ntext are incompatible in the equal to operator...

Here is the code for my course management window. I am relatively new to Qt stuff, basically just learning it off of random forum posts and what I can figure out from the documentation, so I likely have some poor practices in it.

import sys
import configparser
from PyQt5 import QtGui, QtCore, uic
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QTableView, QLabel, QItemDelegate
from PyQt5.QtSql import QSqlTableModel, QSqlDatabase
from PyQt5.QtCore import QVariant, Qt
from views.ui_coursesTable import Ui_Table

class ManageCoursesWindow(QWidget, Ui_Table):
    def __init__(self, db):
        super(ManageCoursesWindow, self).__init__()

        self.config = configparser.ConfigParser()
        self.config.read('config.ini')
        
        self.areEdits=False
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.ui = Ui_Table()
        self.ui.setupUi(self)
        
        self.db = db
        self.db.setDatabaseName(self.config.get('Database', 'conn_string'))
        self.db.open()
        self.model = QSqlTableModel()
        self.initializedModel()
        self.ui.tableView.setModel(self.model)
        self.ui.tableView.setColumnWidth(1,235)
        self.ui.tableView.setColumnWidth(2,100)
        self.ui.tableView.setColumnWidth(9,85)
        self.setWindowTitle("Manage Courses")

        self.ui.addRow.clicked.connect(self.onAddRow)
        self.ui.deleteRow.clicked.connect(self.onDeleteRow)
        self.ui.submitAll.clicked.connect(self.onSave)
        self.ui.filterButton.clicked.connect(self.onFilter)

    def initializedModel(self):
        self.model.setTable("tbl_courses")
        self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        self.model.select()

    def onAddRow(self):
        self.model.insertRows(self.model.rowCount(), 1)
        self.areEdits=True

    def onDeleteRow(self):
        self.model.removeRow(self.ui.tableView.currentIndex().row())
        self.areEdits=True

    def onSave(self):
        self.areEdits=False
        self.model.submitAll()

    def maybeSave (self):
        if self.areEdits:
            if QMessageBox.question(self, 'Save?', "Quit without saving?", QMessageBox.Yes | QMessageBox.No)==QMessageBox.Yes:
                return True
            else:
                return False
        else:
            return True

    def closeEvent(self, event):\
        if maybeSave:
            self.db.close()
            event.accept()

    def onFilter(self):
        course = self.ui.filterField.text().strip()
        self.model.setFilter('')
        self.model.setFilter("Course_Number LIKE '%"+course+"%' OR Course_Title LIKE '%"+course+"%'")
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("Fusion")

    window = ManageCoursesWindow(QSqlDatabase.addDatabase("QODBC"))
    window.show()
    sys.exit(app.exec_())

This software used to be run off of a SQLITE database, but another person that temporarily helped with this software changed it to use a full SQL database a few months ago. I do not remember this error existing back then, although it's possible that we just hadn't happened to use any >128 character links at that point

0

There are 0 best solutions below