开发者

QtSql convert MySql queries to SQLite

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 ,
                   开发者_Python百科     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()


I don't know PyQT, but when you're using the native sqlite bindings, you have to issue

conn.commit()

when you actually want to commit your changes to the database. So you'd need the PyQT version of that after your insert and before your select.

A quick glance at the docs makes it look like the PyQT version is

db.commit()


You do not need to commit in SQLite. Apparently PyQt does not give an error when there is a problem with the SQL syntax.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜