#include <QObject>#include<QSqlDatabase>#include<QSqlError>#include<QSqlQuery>#include<QString>classDatabasemanager: PublicQobject { Public: Databasemanager (Qobject*parent =0); ~Databasemanager (); Public: BOOLopendb (); BOOLDeleteperson (intID); Private: Qsqldatabase db; };
Delete a person from the database:
BOOL Databasemanager::d eleteperson (int ID) { boolfalse; if (Db.isopen ()) { qsqlquery query; = Query.exec (QString ("delete from person where id=%1"). Arg (ID)); } return ret; }
The rest of the code:
BOOLdatabasemanager::opendb () {//Find qslite Driverdb = Qsqldatabase::adddatabase ("Qsqlite"); #ifdef Q_os_linux//Note:we has the to store database file into the user home folder in LinuxQString Path (qdir::home (). path ()); Path.append (Qdir::separator ()). Append ("My.db.sqlite"); Path=qdir::tonativeseparators (path); Db.setdatabasename (path); #else //Note:file exists in the application private folder, in Symbian Qt implementationDb.setdatabasename ("My.db.sqlite"); #endif //Open Databasee returnDb.open (); }
Above reference http://developer.nokia.com/community/wiki/Deleting_data_from_a_database_in_Qt
Select Core Statement
BOOLDatabasemanager::getperson (intID, persondata*&Person ) { BOOLRET =false; Qsqlquery Query (QString ("SELECT * FROM person where id =%1"). Arg (ID)); if(Query.next ()) { person->id = Query.value (0). ToInt (); person->firstname = Query.value (1). toString (); person->lastname = Query.value (2). toString (); person->age = Query.value (3). ToInt (); RET=true; } returnret; }
Above reference http://developer.nokia.com/community/wiki/Searching_for_data_in_a_database_in_Qt
If you have more than one parameter, it can be%1,%2,%3. Arg (). Arg (). Arg () in parentheses to fill in the corresponding variable.
Qt SQL operation, Delete,select