I am trying to convert some MySql queries to SQLite using Python PyQt, but I cannot seem to get it working. When I run the following code, nothing prints out, no errors or results. What is wrong with the code?
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("TEST")
if not db.open():
QMessageBox.warning(None, "Database",
QString("Database Error: %1").arg(db.lastError().text()))
sys.exit(1)
query = QSqlQuery()
query.exec_("""CREATE TABLE IF NOT EXISTS CollectStatus (
id INTEGER NOT NULL AUTO_INCREMENT ,
status TEXT NOT NULL ,
PRIMARY KEY (id) ,
UNIQUE INDEX status_UNIQUE (status ASC) );""")
query.exec_("INSERT INTO CollectStatus (status) VALUES (1)")
query.exec_("SELECT status FROM CollectStatus")
while query.next():
print(query.value(status).toString()[0])
db.close()
You do not need to commit in SQLite. Apparently PyQt does not give an error when there is a problem with the SQL syntax.