Question

I have a problem passing a variable like

query7 = QSqlQuery ("SELECT Exemplo FROM TabelaSubst WHERE Palavra="+text+" ORDER BY Exemple ASC;")

Im getting anything passing my variable text like this. and if i do print query7.next() im getting false..

my program is with PYQT , i have a combobox, when i select some text from it, i pass this text to my variable "text", and i want to use it in my Where Palavra = text, but i dont know how to do it. Im doing it because with it i will get examples of these texts and im putting it in a qtablewidget..

when i do a search with a word, like

query7 = QSqlQuery ("SELECT Exemplo FROM TabelaSubst WHERE Palavra='ronaldo' ORDER BY Exemple ASC;")

.. its ok.. i get my table with examples... But if i change "ronaldo" for my variable text, i get anything..

The way im creating the table to put the examples is:

index1 = 0

while (query7.next()):
        self.tableWidget.setItem(index1,0,QTableWidgetItem(query7.value(0).toString()))  
        index1 = index1+1
Was it helpful?

Solution

Either wrap text in quotes, or put quotes into your query string. For example:

text = "'%s'" % text

or

query7 = QSqlQuery ("SELECT Exemplo FROM TabelaSubst WHERE Palavra='"+text+"' ORDER BY Exemple ASC;")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top