Enabling multiple databases to be opened with QT

Source: Internet
Author: User

1. Requirements

Originally saved data using SQLite memory database, and then need to save configuration information and so on to the database, the program is configured to turn off open to be able to display the normal display saved configuration information

2. Resolve

1. Write the configuration information to the normal configuration file (INI, etc.), the first version of this implementation, the disadvantage of reading and writing files is a bit slow
2. The program uses two kinds of database, the memory database and the file database, the sensitive information is stored in the memory database, and the user configuration information is stored in the file database for long-term preservation.

3. Section code 1. Creating a Memory Database
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE""memory");db.setDatabaseName(":memory:");if (!db.open()) {        LOG_ALL_ERROR(QStringLiteral("failed open sqlite."));        return bret;}
2. Create a file database
QString strdbpath("""/db"dir("");dir"/mysqlite.db";QSqlDatabase dbset = QSqlDatabase::addDatabase("QSQLITE""file");dbset.setDatabaseName(strdbpath);if (!dbset.open()) {    LOG_ALL_ERROR(QStringLiteral("failed open mysqlite.db"));    return bret;}
3. Using the In-memory database
Qsqlquery query (qsqldatabase::d atabase ("Memory", true); bret = Query.exec ("Create TableBacklist (No varchar( -),"                  "Namevarchar( $), Colorvarchar( -),"                  "size varchar( -), RetailDouble,"                  "DiscountDouble, UnitDouble,"                  "Attridvarchar( -), Salenumint,"                  "Backmaxint, Backnumint, TotalpriceDouble,"                  "Serialvarchar( -), OrderIDvarchar( -),"                  "Date varchar( -))");
4. Working with the file database
Qsqlquery setquery (qsqldatabase::d atabase ("file", true); bret = Setquery.exec ("Create TablePrintset (BtitleINTEGER, BshopINTEGER, BbarcodeINTEGER,"    "BsellidINTEGER, BcashdeskINTEGER, BcashierINTEGER,"    "BdateINTEGER, BmemidINTEGER, BscoreINTEGER,"    "BpayableINTEGER, BprivilegeINTEGER, BtotalINTEGER,"    "BgoodnameINTEGER, BgoodcountINTEGER, BunitINTEGER,"    "BsumINTEGER, BdiscountINTEGER, BpriceINTEGER,"    "BcashINTEGER, BbankINTEGER, bchangeINTEGER,"    "BtelINTEGER, BendINTEGER)");
4. Reference

1.http://blog.csdn.net/abeldeng/article/details/24767245 This article describes the creation of not saying use
2.QT Assistant

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Enabling multiple databases to be opened with QT

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.