Solved Whats the problem with this? if i enter wrong ID key error not showing up
-
@Aquarius171 said in Whats the problem with this? if i enter wrong ID key error not showing up:
def searchFunction(self):
self.query = QSqlQuery() self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text())) self.query.addBindValue(self.search.text())
Thats complete nonsense and perfect for SQL injections
If you have a look at the QSqlQuery documentation you could see that forprepare
andaddBindValue()
you need paceholders. -
@jsulm why the else is not working it is always True even if the ID doesn't exist
self.query = QSqlQuery()
self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()))if(self.query.isActive):
print "Active"
else:
print "Not active" -
Where do you execute the query after preparing it?
-
@the_ What do you mean?
-
http://doc.qt.io/qt-5/qsqlquery.html#isActive
As you can see in the docs, a query is only active while executing. So you always will get "Not active" in your code sample.
And: did you have a look at the former link how to correctly use prepared statements?
-
search = self.search.text()
self.query = QSqlQuery() self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(search)) self.query.addBindValue(search)
if(not self.query.exec_()):
print "Not active"
if(self.query.next()):
print "Active"how about this?
-
you did not read/understand how to use prepare correctly?
self.query = QSqlQuery() self.query.prepare("select * from incoming_mac_records where ID=?") self.query.addBindValue(self.search.text()) if not self.query.exec(): print self.query.lastError() else: while self.query.next(): #do whatever you need to do
-
@the_ why this is not showing when i enter wrong input
if not self.query.exec(): #sysntax error so i make it like this self.query.exec_()
print self.query.lastError() -
@the_ and i try to use .isActive() and same happens but not returning error message like print "record not found"
-
@the_
search = self.search.text()self.query = QSqlQuery() self.query.prepare("select * from incoming_mac_records where ID =?") self.query.addBindValue(search) if not self.query.exec_(): print self.query.lastError() else: while self.query.next():
if the ID key is active it show the print statement "active" but if not existing the self.query.lastError() not showing up
-
@Aquarius171 I already told you two times that QSqlQuery::exec() does NOT fail if the id is not found! Do you actually read what others write?
exec() only fails if your query is invalid (wrong SQL syntax) or there is no database connection. If the id is not found exec() will not fail (this is now the third and last time I tell this, sorry, but you really should read answers) - it will execute successfully and the result will be an empty table. A SQL SELECT statement NEVER fails if it does not find anything, instead it returns an empty result. -
@jsulm for that thank you! and sorry :) GodBless sir ^_^ Have a nice day
-
@Aquarius171 No problem! It's just that it is sometimes annoying if you repeat the same several times but the questioner does not read or ignore your explanation :-)
One tip: if you only need to know whether the ID is used use
SELECT count(*) FROM incoming_mac_records WHERE ID =?
-
@jsulm how do i display count?
-
@jsulm how to use isNull( int )?
-
Again: you did not read and/or understand the PyQT QSqlQuery documentation??
-
@Aquarius171 If you're asking questions then please explain better!
Where do you want to display count?
Where do you want to use isNull() and for what data? -
@jsulm thank you already figure it out Thank you and sorry again sir ! :)
-
@the_ i am reading it now haha sorry bro