Using QT to operate SQLite databases (I)-General network code

Source: Internet
Author: User

first, it refers to a conventional Code on the network, but many problems are encountered in actual use, paste the Regular usage as follows:

# Include <qtcore/qcoreapplication> # include <qtsql> # include <qtextcodec> int main (INT argc, char * argv []) {qcoreapplication A (argc, argv); qtextcodec:: setcodecfortr (qtextcodec: codecforlocale (); qsqldatabase dbconn = qsqldatabase: adddatabase ("qsqlite"); // Add the database driver dbconn. setdatabasename ("mytest. DB "); // create a new mytest in the project directory. if (! Dbconn. open () {qdebug () <"fdsfds" ;}qsqlquery query; // run the following QSL statement query.exe C ("create table student (ID varchar, name varchar) "); // create a student table with the ID set as the primary key. There is also a name item query.exe C (qobject: TR (" insert into student values (1, 'aaa ') "); query.exe C (qobject: TR (" insert into student values (2, 'bbb ') "); query.exe C (qobject :: TR ("insert into student values (3, 'ccc ')"); query.exe C (qobject: TR ("insert into student values (3, 'ddd ') "); query.exe C (qobject: TR (" insert into student values (4, 'Eee ') "); query.exe C (qobject :: TR ("insert into student values (5, 'fff')"); query.exe C (qobject: TR ("insert into student values (6, 'ggg ') "); query.exe C (qobject: TR (" select ID, name from student where ID> = 1 "); query.exe C (" select ID, name from student where ID> = 1 "); While (query. next () // query. next () points to the first record found, and moves one record after each time {int ele0 = query. value (0 ). toint (); // query. value (0) is the ID value and converts it to int-type qstring ele1 = query. value (1 ). tostring (); qdebug () <ele0 <ele1; // two output values} query.exe C (qobject: TR ("Drop student ")); return a.exe C ();}

the above method is not good, but there are a lot of inconveniences during use and many problems will occur. I will modify this idea in the following section, and then implement it again in general software development mode.

Related Article

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.