Home > OS >  Using pyqt5 unable to create the SQLite database where there might be the problem?
Using pyqt5 unable to create the SQLite database where there might be the problem?

Time:11-16

The program code and operation result is as follows:
 the from PyQt5. QtSql import QSqlDatabase, QSqlQuery 


Def createDB () :
The db=QSqlDatabase. AddDatabase (" QSQLITE ")
# specified SQLite database file name
Db. SetDatabaseName ('./db/database. Db ')
If not the db. The open () :
Print (' unable to establish connection to the database)
Return False
Query=QSqlQuery ()
Query. The exec (' the create table people (id int primary key, name varchar (10), address varchar (50)) ')
Query. The exec (' insert into people values (1, "li ning", "Shenyang") ')
Query. The exec (' insert into people values (2, "superman", "Nemesis") ')
The close ()
Return True


If __name__=="__main__ ':
CreateDB ()

CodePudding user response:

Modify the code as below:
If __name__=="__main__ ':
App=QApplication (sys. Argv)
CreateDB ()
Sys. Exit (app. Exec_ ())

CodePudding user response:

I also met the same problem, the building Lord solved? Or built a database file, unable to create a table, the file size of 0 KB
  • Related