【Qt数据库操作】使用Qt操作常见的数据库

Qt操作QSQLLITE数据库

使用QSqlDatabase::removeDatabase函数之前要保证连接名对应的QSqlDatabase对象释放或者销毁,否则可能会有内存泄漏。
QSqlDatabase::addDatabase之前要使用QSqlDatabase::contains判断连接名是否存在。存在则使用QSqlDatabase::database。
注意代码中的局部区域,作用是销毁m_QSqlDatabase然后调用removeDatabase。

代码如下:

void MainWindow::on_pushButton_clicked()
{
    // local variable destory m_QSqlDatabase
{
    QSqlDatabase m_QSqlDatabase;
    if(QSqlDatabase::contains("XiaoGongwei_SqlLite"))
        m_QSqlDatabase = QSqlDatabase::database("XiaoGongwei_SqlLite");
    else
        m_QSqlDatabase = m_QSqlDatabase.addDatabase("QSQLITE", "XiaoGongwei_SqlLite");
    m_QSqlDatabase.setDatabaseName("MyDataBase.db");
    if(!m_QSqlDatabase.open())
        qDebug() << "open Sql failed: " << m_QSqlDatabase.lastError();

    QSqlQuery myQuery(m_QSqlDatabase);

    if(!myQuery.exec("drop table student"))
        qDebug() << "QSqlQuery delete table failed: " << myQuery.lastError();

    if(!myQuery.exec("create table student(id int primary key, name text, age int)"))
        qDebug() << "QSqlQuery Sql failed: " << myQuery.lastError();

    for(int i = 0;i < 100;i++)
    {
        QString cmdSql = QString("insert into student VALUES(%1, \"%2\", %3)").arg(i).arg("xiao").arg(99);
        if(!myQuery.exec(cmdSql)){
            qDebug() << "QSqlQuery exec failed: " << myQuery.lastError();
        }
    }
    bool isActiveQuery = false;
    isActiveQuery = myQuery.isActive();
    myQuery.clear();
    isActiveQuery = myQuery.isActive();
    m_QSqlDatabase.close();
    QStringList connNames = m_QSqlDatabase.connectionNames();
    int a = 0;
}
    QSqlDatabase::removeDatabase("XiaoGongwei_SqlLite");// m_QSqlDatabase must delete before removeDatabase()



}

Qt远程操作MySql数据库

当你安装了Appserv或者MySql服务端就会在3306端口开启mysql服务,这样客户端就可以通过远程操作mysql数据库,代码如下:

void MainWindow::on_pushButton_2_clicked()
{
    // local variable destory m_QSqlDatabase
{
    QSqlDatabase m_QSqlDatabase;
    if(QSqlDatabase::contains("XiaoGongwei_SqlLite"))
        m_QSqlDatabase = QSqlDatabase::database("XiaoGongwei_SqlLite");
    else
        m_QSqlDatabase = m_QSqlDatabase.addDatabase("QMYSQL", "XiaoGongwei_SqlLite");
    m_QSqlDatabase.setHostName("103.46.128.***");// IP
    m_QSqlDatabase.setUserName("admin");
    m_QSqlDatabase.setPassword("admin123");
    m_QSqlDatabase.setDatabaseName("test");
    m_QSqlDatabase.setPort(3306);

    if(!m_QSqlDatabase.open())
        qDebug() << "open Sql failed: " << m_QSqlDatabase.lastError();

    QSqlQuery myQuery(m_QSqlDatabase);

    if(!myQuery.exec("drop table student"))
        qDebug() << "QSqlQuery delete table failed: " << myQuery.lastError();

    if(!myQuery.exec("create table student(id int primary key, name text, age int)"))
        qDebug() << "QSqlQuery Sql failed: " << myQuery.lastError();

    for(int i = 0;i < 100;i++)
    {
        QString cmdSql = QString("insert into student VALUES(%1, \"%2\", %3)").arg(i).arg("xiao").arg(i+99);
        if(!myQuery.exec(cmdSql)){
            qDebug() << "QSqlQuery exec failed: " << myQuery.lastError();
        }
    }

    // query data
    myQuery.exec("select * from student");
    if(!myQuery.exec())
    {
        qDebug()<<myQuery.lastError();
    }
    else
    {
        while(myQuery.next())
        {
            int id = myQuery.value(0).toInt();
            QString name = myQuery.value(1).toString();
            int age = myQuery.value(2).toInt();
            qDebug()<<QString("id:%1    name:%2    age:%3").arg(id).arg(name).arg(age);
        }
    }

    //delete data
    myQuery.exec("delete from student where id = 1");
    if(!myQuery.exec())
    {
        qDebug()<<myQuery.lastError();
    }
    else
    {
        qDebug()<<"deleted!";
    }

    bool isActiveQuery = false;
    isActiveQuery = myQuery.isActive();
    myQuery.clear();
    isActiveQuery = myQuery.isActive();
    m_QSqlDatabase.close();
    QStringList connNames = m_QSqlDatabase.connectionNames();
    int a = 0;
}
    QSqlDatabase::removeDatabase("XiaoGongwei_SqlLite");// m_QSqlDatabase must delete before removeDatabase()

}

QSqlTabelModel

    QSqlDatabase m_QSqlDatabase;
    if(QSqlDatabase::contains("XiaoGongwei_SqlLite"))
        m_QSqlDatabase = QSqlDatabase::database("XiaoGongwei_SqlLite");
    else
        m_QSqlDatabase = m_QSqlDatabase.addDatabase("QSQLITE", "XiaoGongwei_SqlLite");
    m_QSqlDatabase.setDatabaseName("MyDataBase.db");
    if(!m_QSqlDatabase.open())
        qDebug() << "open Sql failed: " << m_QSqlDatabase.lastError();

    QSqlQuery myQuery(m_QSqlDatabase);


    QString selectsql ="select * from student";
    myQuery.exec(selectsql);
    if(!myQuery.next())
    {
        if(!myQuery.exec("create table student(id int primary key, name text, age int)"))
            qDebug() << "QSqlQuery Sql failed: " << myQuery.lastError();
        else{
            for(int i = 0;i < 10;i++)
            {
                QString cmdSql = QString("insert into student VALUES(%1, \"%2\", %3)").arg(i).arg("xiao").arg(99);
                if(!myQuery.exec(cmdSql)){
                    qDebug() << "QSqlQuery exec failed: " << myQuery.lastError();
                }
            }
        }
    }
    else{

    }

    mp_QSqlTableModel = new QSqlTableModel(this, m_QSqlDatabase);
    ui->tableView->setModel(mp_QSqlTableModel);

    mp_QSqlTableModel->setTable("student");

    bool isgood = mp_QSqlTableModel->select();
    if(!isgood)
        qDebug() << "QSqlTableModel select failed: " << mp_QSqlTableModel->lastError();
    mp_QSqlTableModel->setHeaderData(0, Qt::Horizontal, "学号");

你可能感兴趣的:(Ubuntu下QT开发,Windows,QT,Qt,数据库,sqlite,mysql,phpmyadmin)