Home > Mobile >  QSqlDatabase perform open times wrong???????
QSqlDatabase perform open times wrong???????

Time:01-09

Logical process:
1. To determine whether there is a database file, there is no is created;
2. Determine whether there is a corresponding table, does not exist is created;
The code is as follows:
 
# include "widget. H"
# include "ui_widget. H"
# include & lt; QDebug>

# define FILE_NAME "ThreeKindoms. Db
"
Widgets: : widgets (QWidget * parent) :
QWidget (parent),
UI (new UI: : Widget)
{
The UI - & gt; SetupUi (this);
If # 1
//to determine whether a file exists
QFileInfo the fileInfo (QString (QCoreApplication: : applicationDirPath () + "/" + FILE_NAME));
if (! The fileInfo. The exists ()) {
//create a file
This - & gt; CreateDBFile ();
}
QString shuTable="ShuKingdom";
This - & gt; CreateTable (shuTable);
# the else
This - & gt; CreateDBFile ();
# endif
}

Widgets: : ~ Widget ()
{
delete ui;
}

Void Widget: : createDBFile ()
{
M_database=QSqlDatabase: : addDatabase (" QSQLITE ", "ThreeKindoms");
M_database. SetDatabaseName (QString (QCoreApplication: : applicationDirPath () + "/" + FILE_NAME));
if (! M_database. The open ())
QDebug () & lt; <"Open the database file failed.";
The else
QDebug () & lt; <"Open the database file success.";
}

Void Widget: : createTable (QString & amp; TableName)
{
if (! M_database. IsOpen ()) {
if (! M_database. The open ())
QDebug () & lt; <"ERROR:" & lt; }
.
}


:
When the compiler to run for the first time, the console print for
 
Open the database file success.
Create "ShuKingdom" success.


Again at this point, the running project, will appear the following print tip:
 ERROR: QSqlError (" ", "Driver not the loaded", "Driver not the loaded") 


Would you please tell me why you a great god, and compile run once, again running would error QSqlError error; But this error is in the createTable function execution whether m_database open, did not open the execution of the open, why go wrong here?
  •  Tags:  
  • Qt
  • Related