Qt SQL operation, Delete,select

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.