Qt学习笔记-SQL的基本操作【创建、查询、添加、索引等】

程序运行截图如下:




代码如下:

connection.h

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlQuery>
#include <QSqlDatabase>

static bool createConnection(){
    QSqlDatabase db1=QSqlDatabase::addDatabase("QSQLITE","connection1");
    db1.setDatabaseName("my1.db");
    if(!db1.open()){
        QMessageBox::critical(NULL,"提示","数据库打开失败!");
        return false;
    }
    QSqlQuery query(db1);
    query.exec("create table student (id int primary key, "
               "name varchar(20))");
    query.exec("insert into student values(0, 'LiMing')");
    query.exec("insert into student values(1, 'LiuTao')");
    query.exec("insert into student values(2, 'WangHong')");
    return true;
}

#endif // CONNECTION_H

main.cpp

#include <QApplication>
#include <QDebug>
#include <QSqlDatabase>
#include <QStringList>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlDriver>
#include <QSqlError>
#include "connection.h"

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

    if(!createConnection()){
        qDebug()<<"连接失败!";
        return 1;
    }

    QSqlDatabase db1=QSqlDatabase::database("connection1");
    QSqlQuery query(db1);
    qDebug()<<"connection1:";
    query.exec("select * from student");
    while(query.next()){
        qDebug()<<query.value(0).toInt()<<query.value(1).toString();
    }

    int numRows;

    //获取行号
    if(db1.driver()->hasFeature(QSqlDriver::QuerySize)){
        qDebug()<<"QSqlDriver::QuerySize";
        numRows=query.size();
    }
    else{
        qDebug()<<"unQSqlDriver::QuerySize";
        query.last();
        numRows=query.at()+1;
    }

    qDebug()<<"row number:"<<numRows;

    //指向索引
    query.seek(1);
    qDebug()<<"current index:"<<query.at();
    qDebug()<<"current data:"<<query.value(0).toInt()<<query.value(1).toString();

    //获取当前行的记录
    QSqlRecord record=query.record();
    int id=record.value("id").toInt();
    QString name=record.value("name").toString();
    qDebug()<<"id:"<<id<<"name:"<<name;

    //获取索引为1的字段,即第二个字段
    QSqlField field=record.field(1);
    qDebug()<<"second field:"<<field.name()<<"field value:"<<field.value().toString();

    //SQL查询 方式1:
    query.prepare("insert into student (id,name) values (:id,:name)");
    int idValue=100;
    QString nameValue="CSDN IT1995";
    query.bindValue(":id",idValue);
    query.bindValue(":name",nameValue);
    query.exec("select * from student");
    while(query.next()){
        qDebug()<<query.value(0).toInt()<<query.value(1).toString();
    }

    //SQL查询 方式2:
    query.prepare("insert into student (id,name) values (?,?)");
    int idValue1=101;
    QString nameValue1="CSDN IT1995 two";
    query.addBindValue(idValue1);
    query.addBindValue(nameValue1);
    query.exec();
    query.exec("select * from student");
    while(query.next()){
        qDebug()<<query.value(0).toInt()<<query.value(1).toString();
    }

    //SQL查询 方式3:
    query.prepare("insert into student (id,name) values (?,?)");
    QVariantList ids;
    ids<<20<<21<<22;
    query.addBindValue(ids);
    QVariantList names;
    names<<"球球"<<"腿腿"<<"闰土";
    query.addBindValue(names);
    if(!query.execBatch())
        qDebug()<<query.lastError();
    query.exec("select * from student");
    while(query.next()){
        qDebug()<<query.value(0).toInt()<<query.value(1).toString();
    }


    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/80753781