日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2025/3/15 数据库 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt学习笔记-SQL的基本操作【创建、查询、添加、索引等】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

程序運(yùn)行截圖如下:




代碼如下:

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,"提示","數(shù)據(jù)庫打開失敗!");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;//獲取行號(hào)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();//獲取當(dāng)前行的記錄QSqlRecord record=query.record();int id=record.value("id").toInt();QString name=record.value("name").toString();qDebug()<<"id:"<<id<<"name:"<<name;//獲取索引為1的字段,即第二個(gè)字段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(); }

總結(jié)

以上是生活随笔為你收集整理的Qt学习笔记-SQL的基本操作【创建、查询、添加、索引等】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。