码迷,mamicode.com
首页 > 数据库 > 详细

QT4.8.5 连接数据库(读写数据)

时间:2017-09-22 12:07:00      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:读写   tsql   数据   locale   打开   bug   qfile   each   ddb   

#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("连接数据库成功。。!"));
        return true;
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    /*测试链接数据库
    //测试支持的数据库
    //    QCoreApplication a(argc, argv);
    //        qDebug() << "Available drivers:";
    //        QStringList drivers = QSqlDatabase::drivers();
    //        foreach(QString driver, drivers)
    //        qDebug() << "\t "<< driver;
    //        qDebug() << "End";
    //    return a.exec();

    //测试打开数据库
    //    QCoreApplication a(argc, argv);
    //           QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //           db.setHostName("localhost");
    //           db.setDatabaseName("qttest");//已建立的数据库名称
    //           db.setUserName("root");//用户名称
    //           db.setPassword("root");//密码
    //           bool ok = db.open();//如果成功ok位true,否则为false
    //           qDebug() << "\t"<<ok;

    qDebug() << "Available drivers:";
        QStringList drivers = QSqlDatabase::drivers();
        foreach(QString driver, drivers)           qDebug() << "\t "<< driver;
        qDebug() << "End";

    */

    //测试连接并打开操作数据库
    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 << "计算机" << "艺术" << "数学" << "外语";
        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", "测试");
        query.bindValue(":description", "测试插入数据");
        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();
}

QT4.8.5 连接数据库(读写数据)

标签:读写   tsql   数据   locale   打开   bug   qfile   each   ddb   

原文地址:http://www.cnblogs.com/yingchuanxiaoge/p/7573646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!