QT's Operational database (SQLite) instance

Source: Internet
Author: User
Tags driver database ibm db2 prepare sqlite sqlite database postgresql version



The Qtsql module provides an interface to the SQL database that is independent of the platform and the type of database, supported by a set of classes that integrate the database with the user interface using the QT Model view architecture.
The Qsqldatabase object symbolizes the association of the database. QT uses drivers to communicate with the application programming interfaces of various databases. The desktop version of QT (Edition) includes some of the following drivers:

Driver database
QDB2 IBM DB2 version 7.1 and later
Qibase Borland InterBase
Qmysql MYSQL
QOCI Oracle Inc. (Oracle call Interface)
QODBC ODBC (including Microsoft's QSL services)
Qpsql PostgreSQL version 7.3 and later
Qsqlite Qslite 3rd Edition
QSQLITE2 Qslite 2nd Edition



Qtds qybase Adaptive Server





To access an instance of the Qsqlite database: Step 1, create a QT console program. 2, add the QT +=sql;3 in the pro ask price, add the following code in the Main.cpp
#include <QCoreApplication>
#include <QSql>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>
#include <QFile>
#include <QDebug>
#include <QVariantList>
  
int main (int argc, char * argv [])
{
    QCoreApplication a (argc, argv);
  
    QSqlDatabase database = QSqlDatabase :: addDatabase ("QSQLITE");
  
    database.setDatabaseName ("CashSystem.db");
  
    if (database.open ())
    {
        qDebug () << "Database Opened";
  
        QSqlQuery sql_query;
        QString create_sql = "create table member (id int primary key, name varchar (30), address varchar (30))"; // Create a data table
        QString insert_sql = "insert into member values (?,?,?)"; // Insert data
  
        QString select_all_sql = "select * from member";
  
        sql_query.prepare (create_sql); // Create table
        if (! sql_query.exec ()) // Check if the table creation was successful
        {
            qDebug () << QObject :: tr ("Table Create failed");
            qDebug () << sql_query.lastError ();
        }
        else
        {
            qDebug () << "Table Created";
  
            // Insert data
            sql_query.prepare (insert_sql);
  
            QVariantList GroupIDs;
            GroupIDs.append (0);
            GroupIDs.append (1);
            GroupIDs.append (2);
  
            QVariantList GroupNames;
            GroupNames.append ("hsp");
            GroupNames.append ("rl");
            GroupNames.append ("spl");
  
            QVariantList GroupAddress;
            GroupAddress.append ("Nanchong");
            GroupAddress.append ("Baoji");
            GroupAddress.append ("Nanchong");
  
            sql_query.addBindValue (GroupIDs);
            sql_query.addBindValue (GroupNames);
            sql_query.addBindValue (GroupAddress);
  
            if (! sql_query.execBatch ())
            {
                qDebug () << sql_query.lastError ();
            }
            else
            {
                qDebug () << "Insert record successfully";
            }
  
            // Query all records
            sql_query.prepare (select_all_sql);
            if (! sql_query.exec ())
            {
                qDebug () << sql_query.lastError ();
            }
            else
            {
                while (sql_query.next ())
                {
                    int id = sql_query.value (0) .toInt ();
                    QString name = sql_query.value (1) .toString ();
                    QString address = sql_query.value (2) .toString ();
                    qDebug () << QString ("ID:% 1 Name:% 2 Address:% 3"). arg (id) .arg (name) .arg (address);
                }
            }
        }
    }
    database.close ();
   // QFile :: remove ("CashSystem.db");
    return a.exec ();
} 
4. Operation:




5. Under the Debug folder of the project, a corresponding. db file is generated, opened with the Navicat Forsqlite tool and the results are as follows:








Reference Links:



1, the QT Operation database (SQLite) instance



2, Qt---with the database qsqlite



3. Use SQLite database in QT






QT's Operational database (SQLite) instance


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.