"Introduction to Brief features"
Use SQLite database and QT build interface to realize contact database record. Contact person has ID number, name, age. Can contact
"Table Add Contact"
"Delete Contacts"
"Update contact information"
"Show All Contact information"
"Run Interface"
"Source Download" "Http://pan.baidu.com/s/1o6OGley"
Use Sqlitebrowser to view
"Initialize Database"
Qlibrary Sqlib ("./sqldrivers/qsqlited.dll"); Sqlib.load (); Qdebug ()<<"My library loaded"<<sqlib.isloaded (); DB= Qsqldatabase::adddatabase ("Qsqlite","SQLITE"); #ifdef q_os_linux QString Path (qdir::home (). path ()); Path.append (Qdir::separator ()). Append ("My.db.sqlite"); Path=qdir::tonativeseparators (path); Db.setdatabasename (path);#elseDb.setdatabasename ("My.db.sqlite");#endif /** Create My.db.sqlite file if not exist*/Db.open (); Db.close ();
"CREATE TABLE"
if (Db.open ()) { sql_query = qsqlquery (db); Sql_query.prepare (create_sql); if (!sql_query.exec ()) { qdebug () <<sql_query.lasterror (); } else { qdebug () << "Table created!"; } } Updatedatabaseshow ();
"Insert Record"
if(Db.open ()) {Sql_query=qsqlquery (DB); intmax_id =0; Sql_query.prepare (Select_max_sql); if(!sql_query.exec ()) {Qdebug ()<<Sql_query.lasterror (); } Else { while(Sql_query.next ()) {max_id= Sql_query.value (0). ToInt (); Qdebug ()<<qstring ("Max id:%1"). Arg (MAX_ID); }} sql_query.prepare (Insert_sql); Sql_query.addbindvalue (max_id+1); Sql_query.addbindvalue (UI->lineedit_name->text ()); Sql_query.addbindvalue (UI->lineedit_age->text (). ToInt ()); if(!sql_query.exec ()) {Qdebug ()<<Sql_query.lasterror (); } Else{qdebug ()<<"inserted!"; } updatedatabaseshow (); }
"Show all data (two columns, ID and name)"
if(Db.open ()) {Sql_query=qsqlquery (DB); if(!sql_query.exec (Select_sql)) {Qdebug ()<<Sql_query.lasterror (); } Else { while(Sql_query.next ()) {intid = sql_query.value ("ID"). ToInt (); QString name= Sql_query.value ("name"). toString (); Qdebug ()<<qstring ("id:%1 name:%2"). Arg (ID). ARG (name); } } }
"Show All Data"
if(Db.open ()) {Sql_query=qsqlquery (DB); Sql_query.prepare (Select_all_sql); if(!sql_query.exec ()) {Qdebug ()<<Sql_query.lasterror (); } Else{UI->textedit->Clear (); while(Sql_query.next ()) {intid = sql_query.value (0). ToInt (); QString name= Sql_query.value (1). toString (); intAge = Sql_query.value (2). ToInt (); UI->textedit->append (QString ("id:%1 name:%2 age:%3"). Arg (ID). ARG (name). Arg (age)); Qdebug ()<<qstring ("id:%1 name:%2 age:%3"). Arg (ID). ARG (name). Arg (age); } } }
"Clear Table"
if (Db.open ()) { = qsqlquery (db); Sql_query.prepare (clear_sql); if (! sql_query.exec ()) { qdebug ()<<sql_query.lasterror (); } Else { qdebug ()<<"cleared"; } } Updatedatabaseshow ();
"Delete a record" ComboBox selected record deleted
if (Db.open ()) { = qsqlquery (db); Sql_query.prepare (delete_sql); Sql_query.addbindvalue (UI->combobox->Currenttext (). ToInt ()); if (! sql_query.exec ()) { qdebug ()<<sql_query.lasterror (); } Else { qdebug ()<<"deleted! " ; } } Updatedatabaseshow ();
"Update contact information"
if(Db.open ()) {Sql_query=qsqlquery (DB); Sql_query.prepare ("Update Student Set name =: Name, age =: Age WHERE id =: ID"); Sql_query.bindvalue (": Name", ui->lineedit_name->text ()); Sql_query.bindvalue (": Age", ui->lineedit_age->text ()); Sql_query.bindvalue (": ID", ui->combobox->Currenttext (). ToInt ()); if(!sql_query.exec ()) {Qdebug ()<<Sql_query.lasterror (); } Else{qdebug ()<<"updated!"; }} updatedatabaseshow ();
"Delete Database"
#ifdef Q_os_linux QString Path (Qdir::home (). path ()); Path.append (Qdir::separator ()). Append ("my.db.sqlite"); = qdir::tonativeseparators (path); Qfile::remove (path); #else qfile::remove ("my.db.sqlite"); #endif UI->combobox->Clear (); UI->textedit->clear ();
QT Database Usage List "Contacts"--using SQLite and Qstringlistmodel