How can I filter an ms-access databse, using QSqlTableModel and QLineEdit?

2024/11/15 9:18:30

I'm building a GUI that allows users to search information in a ms access database (yup. It has to be the ms access) The user has a textfield where he can type his search and the Tableview should update instantly. At the moment the DB disappears whenever you type a letter in the field.

Took me a while to figure out the problem: my SQL statement is simply not right. (Thanks to model.lastError)

The whole function looks like this:

    self.ui.Kistenschild_suchen.textChanged.connect(self.update_filter)def update_filter(self, s):s = re.sub("[\W_]+", "", s)filter_str = 'Produkt LIKE %{}%"'.format(s)self.ui.model.setFilter(filter_str)print(self.ui.model.lastError())

In this case I typed k

The errormessage is:

PySide6.QtSql.QSqlError("-3100", "QODBC: Unable to execute statement", "[Microsoft][ODBC-Treiber für Microsoft Access] Syntaxfehler in Abfrageausdruck 'Produkt LIKE (%h%\")'.") at 0x000001CA4FB33B88>

Point of interest should be the '%h%")'."' Since it shows more characters than typed

I tried to change in several ways, like changing the % to * and? Still nothing

EDIT:

Here is the minimal reproducible example:

import re
import sysfrom PySide6.QtCore import QSize, Qt
from PySide6.QtSql import QSqlDatabase, QSqlTableModel
from PySide6.QtWidgets import (QApplication,QLineEdit,QMainWindow,QTableView,QVBoxLayout,QWidget,
)Driver= r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\scripts\python\pyside_Tutorials\databases\chinook.accdb'db = QSqlDatabase("QODBC")
db.setDatabaseName(Driver)
db.open()class MainWindow(QMainWindow):def __init__(self):super().__init__()container = QWidget()layout = QVBoxLayout()self.search = QLineEdit()self.search.textChanged.connect(self.update_filter)self.table = QTableView()layout.addWidget(self.search)layout.addWidget(self.table)container.setLayout(layout)self.model = QSqlTableModel(db=db)self.table.setModel(self.model)self.model.setTable("Track")self.model.select()self.setMinimumSize(QSize(1024, 600))self.setCentralWidget(container)# tag::filter[]def update_filter(self, s):s = re.sub("[\W_]+", "", s)filter_str = 'Name LIKE "%{}%"'.format(s)self.model.setFilter(filter_str)print(self.model.lastError())print(s,type(s))# end::filter[]app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

This code gives me the errormessage: <PySide6.QtSql.QSqlError("-3010", "QODBC: Unable to execute statement", "[Microsoft][ODBC-Treiber für Microsoft Access] 1 Parameter wurden erwartet, aber es wurden zu wenig Parameter übergeben.") at 0x0000016FC7535108>

Which means something like: "1 parameter was expected but too few parameters were passed"

Answer

MS-Access needs a double apostrophe like:

def update_filter(self, s):s = re.sub(r"[\W_]+", "", s)filter_str = f"Produkt '%{s}%'"self.model.setFilter(filter_str)```
https://en.xdnf.cn/q/119335.html

Related Q&A

Python regex - Replace single quotes and brackets

Id like to replace quantities with name then a square bracket and a single quote with the contents inside. So, from this: RSQ(name[BAKD DK], name[A DKJ])to this:RSQ(BAKD DK, A DKJ)

Python unexpected EOF while parsing (python2.7)

Heres my python code. Could someone show me whats wrong with it? I try to learn an algorithm on solving 24 - point game. But I really dont know why this program has an error.from __future__ import div…

Call a function in repl without brackets

Would like to know if there is a way to call a function in python in repl just with the function name. $ python -i interace.py >>> load 834.png >>> sharpen >>> saverather tha…

how to work on a exist session in selenium with python?

I want to fill some field of a webpage and then send a request to it but this website has a very powerful login page to avoid sending requests for login from a robot so I cant log in with selenium bu…

how to find similarity between many strings and plot it

I have a xls file with one column and 10000 strings I want to do few things 1- make a heatmap or a cluster figure shows the similarity percentage between each string with another one.In order to find …

Python and Variable Scope

So I am recently new to Python, but I seem to be able to program some stuff and get it working. However Ive been trying to expand my knowledge of how things work in the language, and putting this simp…

How to check if element is orthogonally adjacent (next to) to existing elements?

Im trying to make a simple game where a building placed in a nested list must be next to another building. The problem I face is that if the building was placed at the sides, I cant use for loops to ch…

How to add new column(header) to a csv file from command line arguments

The output of the following code:-import datetime import csv file_name=sample.txt with open(file_name,rb) as f: reader = csv.reader(f,delimiter=",") …

Pattern matching and replacing in Python

Im trying to take a string that can be anything like "Hello here is a [URL]www.url.com[/URL] and its great." and be able to extract whatever is between [URL] and [/URL] and then modify the st…

In Python word search, searching diagonally, printing result of where word starts and ends

I have a friend of mine tutoring me in learning Python and he gave me this project where a user will read a word search into the program and the file includes a list of words that will be in the word s…