Home > Mobile >  Program database operations, handle a lot of problems
Program database operations, handle a lot of problems

Time:10-07

hello, I want to ask you a question, help me use the QT platform, developed a relevant database program, the way is online see basic database operations, I wrote a first experiment program, there is no problem, have saved one million data, are all normal operation, then the code, I tested in my new program, there was a problem,
In the Widget class, I add a callback function, if you receive send my hardware interrupts, first triggered the callback function, the callback function is a static nonmember functions, the callback function is triggered, will send events to the Widget class a member function of this member function of the Widget class, concrete action to save is to get the data, save the data code is my experiment before the database code, the whole program can be run normally, but there will be a big problem, is the program had been applying for many handle, as to save the amount of data, the greater the amount of handle more and more, in the end, the handle of the whole system has been used up, the system can't do anything, I saw the task manager, memory consumption, CPU also have no, is to handle in the increase of the crazy, programs run on Windows 7 system, operating system, directly prompt system resources exhausted, what all points not, finally can only restart the computer,
Database operation part about logic is as follows:

 QSqlDatabase database=QSqlDatabase: : addDatabase (" QSQLITE "); 
Database. SetDatabaseName (" VMIC_DataBase. Db ");
The database transaction ();
For (int I=0; I & lt; NUM. I++)
{
QString qsStr;
QsStr. Sprintf (" insert into member values (' % s', % d) ", nDatabaseID, cRecBufAssem [I]);
Bool success=query. The exec (qsStr);
}
if(! QSqlDatabase: : database (). The commit ())
{
QDebug () & lt; if(! QSqlDatabase: : database (). The rollback ())
{
QDebug () & lt; }
}

through my screening, its consumption should handle is this:
Bool success=query. The exec (qsStr);
I want to ask you, why will appear this kind of circumstance, I before the experiment procedure, also have this statement, how no handle to consumption, how to solve this problem, how to apply for or to release handle,
I am a novice, not skilled to QT, please answer, a little detail, thank you, thank you, online, etc.,

CodePudding user response:

Is usually the main function to perform database is opened, the last program close when database, instead of using the database every time opening and closing time! That need to use handle every time!

CodePudding user response:

reference 1st floor feiyangqingyun response:
is the main function commonly perform database is opened, the last program close when database, instead of using the database every time opening and closing time! That need to use handle every time!

I didn't, I, of course, not every time to open, save the data, and then shut down, so there is no need to ah, I the whole operation executed only once, this I know,

CodePudding user response:

No context, no judgment, I changed to such general, you try again
 QSqlDatabase database=QSqlDatabase: : database (" QSQLITE "); 
If (database. IsValid ()==false) {
The database=QSqlDatabase: : addDatabase (" QSQLITE ");
Database. SetDatabaseName (" VMIC_DataBase. Db ");
}

QSqlQuery query;
Query. Prepare (" insert into member values (?,?,?,?,? ,?) ");
The database transaction ();
For (int I=0; I & lt; NUM. I++)
{
Query. BindValue (0, nDatabaseID, QSql: : In);
Query. BindValue (1, cRecBufAssem [I], QSql: : In);
Bool success=query. The exec ();
}

if(! QSqlDatabase: : database (). The commit ())
{
QDebug () & lt; if(! QSqlDatabase: : database (). The rollback ())
{
QDebug () & lt; }
}

CodePudding user response:

 if (! QSqlDatabase: : database (). The commit ()) 
{
QDebug () & lt; if(! QSqlDatabase: : database (). The rollback ())
{
QDebug () & lt; }
}

This forget changed, changed to:
 if (! MIT ()) 
database.com{
QDebug () & lt; if(! The database. The rollback ())
{
QDebug () & lt; }
}

CodePudding user response:

The
reference 4 floor donwmufromdying reply:

friend, thank you for your enthusiastic help, I tried, still won't do, I think, whether with a callback, if the callback function in essence and the Widget is not a thread, opens the multithreaded what caused the accident consequences,

CodePudding user response:

refer to fifth floor program voice response:
Quote: refer to 4th floor donwmufromdying response:

friend, thank you for your enthusiastic help, I tried, still won't do, I think, whether with a callback, if the callback function in essence and the Widget is not a thread, opens the multithreaded what caused the accident consequences,


Callback function, of course, most likely and the widget is not the same thread,,,
You print the thread ID know,
QDebug () & lt;
Multi-threaded database operation is not safe!!!!!!
So suggest building a database operations or other worker threads to manage,
UI need database operations, just give it a signal and let the signal line up in the queue

CodePudding user response:

Database thread safe I don't confirm QT did not, that you'd better screening,
  •  Tags:  
  • Qt
  • Related