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

歡迎訪問 生活随笔!

生活随笔

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

数据库

Qt学习(十一):QT设置静态数据库

發(fā)布時(shí)間:2025/3/12 数据库 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt学习(十一):QT设置静态数据库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

知識(shí)點(diǎn)

如果數(shù)據(jù)量少的情況下,不需要連接本地mysql,自己創(chuàng)建一個(gè)db文件就行
完整項(xiàng)目github地址:

https://github.com/taw19960426/Qt_study/tree/main/SqlLite

widget.cpp

#include "widget.h" #include "ui_widget.h" #include <QSqlDatabase> #include <QDebug> #include <QMessageBox> #include <QSqlError> #include <QSqlQuery> #include <QVariantList>#define cout qDebug() << "[" << __FILE__ <<":" << __LINE__ << "]"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);qDebug() << QSqlDatabase::drivers();//添加Sqlite數(shù)據(jù)庫(kù)QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//設(shè)置數(shù)據(jù)庫(kù)db.setDatabaseName("./info.db");//打開數(shù)據(jù)庫(kù)if( !db.open() ) //數(shù)據(jù)庫(kù)打開失敗{QMessageBox::warning(this, "錯(cuò)誤", db.lastError().text());return;}QSqlQuery query;bool ansCra=query.exec("create table student(id int primary key, name varchar(255), age int, score int);");if(!ansCra){cout<<"執(zhí)行數(shù)據(jù)庫(kù)命令出錯(cuò)";}query.prepare("insert into student(name, age, score) values(?, ?, ?)");//給字段設(shè)置內(nèi)容 listQVariantList nameList;nameList << "xiaoming" << "xiaolong" << "xiaojiang";QVariantList ageList;ageList << 11 << 22 << 33;QVariantList scoreList;scoreList << 59 << 69 << 79;//給字段綁定相應(yīng)的值 按順序綁定query.addBindValue(nameList);query.addBindValue(ageList);query.addBindValue(scoreList);//執(zhí)行預(yù)處理命令bool ansPi=query.execBatch();if(!ansPi){cout<<"執(zhí)行數(shù)據(jù)庫(kù)命令出錯(cuò)";}bool ansSel=query.exec("select * from student");if(!ansSel){cout<<"執(zhí)行數(shù)據(jù)庫(kù)命令出錯(cuò)";}while(query.next()) //一行一行遍歷{//取出當(dāng)前行的內(nèi)容qDebug() << query.value(0).toInt()<< query.value(1).toString()<< query.value("age").toInt()<< query.value("score").toInt();} }Widget::~Widget() {delete ui; }

總結(jié)

以上是生活随笔為你收集整理的Qt学习(十一):QT设置静态数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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