Using QT to operate SQLite databases (2) -- Problems and Solutions

Source: Internet
Author: User

In the previous section, I found a general example on the network for you. Next I will gradually change the code into reusable code available in conventional development.

First, let's talk about the problems encountered by the above Code in actual use and how to get the results.

1. After creating a program, the system prompts "qsqldatabase: no such file or directory" error.

2. In most instances, you can directly add a database by declaring qsqldatabase dbconn = qsqldatabase: adddatabase ("qsqlite") in the main function. The scope of the created qsqldatabase object has problems, that is, the problem of using this local object in other functions or such objects,
In other non-dbconn scope functions, you can obtain the result through qsqlquery In the debug debugging version, but the result cannot be obtained in non-Debug debugging versions.

3. Use the following method when using qsqlquery:

{.....    QSqlQuery query;    query.exec("create table student(id varchar,name varchar)"); .....}

First, the premise that this statement can be executed is in the same function (in the preceding example, the main function), and the declared qsqldatabase object is in the same scope;

If the execution result of qsqlquery can be obtained in the debug debugging status when other functions or scopes of non-qsqldatabase objects are involved, however, the desired results will not be obtained in the non-Debug debugging status (for example, the SELECT statement is executed through qsqlquery)

4. If qsqlquery does not have any parameters, how can I find the SQL statement of the database I want to execute when running the exec function?

 

The following solutions are available for the above problems:

1. You can add QT + = SQL to the Project PRO file to enable the project (or Lib) to support database operations.

2. You can create a qsqldatabase pointer using the following code and save the pointer in the class to solve the scope problem,

QSqlDatabase * m_sqliteDB= new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "dataSqlite"));

To prevent qsqlquery from failing to query results, it is best to specify the Link name (ConnectionnameIs the second parameter of the code above.

3. After using solution 2, solution 3 is easy to solve. Add the qsqldatabase parameter when declaring qsqlquery. The Code is as follows:

QSqlQuery query(*m_sqliteDB);

M_sqlitedb is a reference of the pointer created in the 2 solution.

Then call the exec function of query to execute the code.

4. When the solution 3 is used, it does not exist because no parameter object is used for qsqlquery in different functions, when calling its exec function directly, I wondered why I didn't explain which database I was using.

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.