QT4.8.5 connect to the database (read and write data) and qt4.8.5 read and write

Source: Internet
Author: User

QT4.8.5 connect to the database (read and write data) and qt4.8.5 read and write

#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QPushButton>
 
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QSpinBox>
#include <QSlider>
#include <QHBoxLayout>
#include  <QFile>
#include <QFileInfo>
#include <QDebug>
#include "newspaper.h"
#include "reader.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include <QTextCodec>
 
bool connect(const QString &dbName)
{
 
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName(dbName);
    db.setPort(3306);
    db.setUserName("root");
    db.setPassword("root");
    if (!db.open())
    {
        QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
        return false;
    }
    else
    {
QMessageBox: information (0, QObject: tr ("Tips"), QObject: tr ("connection successful ..! "));
        return true;
    }
}
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
/* Test the connected database
// Test the supported Databases
    //    QCoreApplication a(argc, argv);
    //        qDebug() << "Available drivers:";
    //        QStringList drivers = QSqlDatabase::drivers();
    //        foreach(QString driver, drivers)
    //        qDebug() << "\t "<< driver;
    //        qDebug() << "End";
    //    return a.exec();
 
// Test to open the database
    //    QCoreApplication a(argc, argv);
    //           QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //           db.setHostName("localhost");
// Db. setDatabaseName ("qttest"); // name of the created database
// Db. setUserName ("root"); // User Name
// Db. setPassword ("root"); // Password
// Bool OK = db. open (); // If OK is set to true, otherwise false
    //           qDebug() << "\t"<<ok;
 
    qDebug() << "Available drivers:";
        QStringList drivers = QSqlDatabase::drivers();
        foreach(QString driver, drivers)           qDebug() << "\t "<< driver;
        qDebug() << "End";
 
    */
 
// Test the connection and open the operation Database
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
 
 
 
    if (connect("eng"))
    {
        QSqlQuery query;
 
        /*
        query.prepare("INSERT INTO student  VALUES (?, ?, ?, ?)");
        QVariantList IDs;
        IDs << 1 << 2 << 3 << 4;
        query.addBindValue(IDs);
 
        QVariantList names;
        names << "Tom" << "Jack" << "Jane" << "Jerry";
        query.addBindValue(names);
 
        QVariantList dept_names;
Dept_names <"computer" <"art" <"Mathematics" <"Foreign Language ";
        query.addBindValue(dept_names);
 
        QVariantList tot_creds;
        tot_creds << "10" << "11" << "12" << "13";
        query.addBindValue(tot_creds);
 
        if (!query.execBatch())
        {
            QMessageBox::critical(0, QObject::tr("Database Error"),
                                  query.lastError().text());
        }
        query.finish();
        */
 
        query.prepare("INSERT INTO tempTest (name, description) "
                      "VALUES (:name, :description)");
 
Query. bindValue (": name", "test ");
Query. bindValue (": description", "test data insertion ");
        query.exec();
 
        QString sql="SELECT name, description FROM tempTest";
        query.exec(sql);
        while (query.next())
        {
            QString name = query.value(0).toString();
            QString description = query.value(1).toString();
            qDebug() << name << ": " << description;
        }
 
 
    }
    else
    {
        return 1;
    }
 
 
 
    return a.exec();
}

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.