Qt-數據庫操作SQLite


1  簡介

參考視頻:https://www.bilibili.com/video/BV1XW411x7NU?p=88

說明:本文對在Qt中操作SQLite做簡要說明。

SQLite:SQLite 是一個軟件庫,實現了自給自足的、無服務器的、零配置的、事務性的 SQL 數據庫引擎。具體的操作命令可參考:https://www.runoob.com/sqlite/sqlite-tutorial.html

在Qt中操作SQLite不需要我們單獨先安裝它,可以直接使用。

2  測試及說明

語法就不介紹了,這里說明功能:創建一個info.db數據庫,插入一些數據,然后遍歷輸出。

代碼步驟說明:

(1)添加sqlite數據庫

1 db = QSqlDatabase::addDatabase("QSQLITE");

(2)創建數據庫

語法和之前操作MySql一樣:

1 QSqlQuery query;
2     query.exec("create table student(id int primary key, name varchar(255), age int, score int);");

(3)批量插入條目

 1     //批量插入:odbc風格
 2     //預處理語句
 3     query.prepare("insert into student(name, age, score) values(?, ?, ?);");
 4     // 給字段設置內容
 5     QVariantList nameList;
 6     nameList << "xiaoming" << "xiaokong" << "xiaojiang";
 7     QVariantList ageList;
 8     ageList << 22 << 21 << 24;
 9     QVariantList scoreList;
10     scoreList << 89 << 99 << 78;
11     //給字段綁定相應的值,必須按順序綁定
12     query.addBindValue(nameList);
13     query.addBindValue(ageList);
14     query.addBindValue(scoreList);
15     //執行預處理命令
16     query.execBatch();

(4)遍歷輸出

1     query.exec("select * from student");
2     while (true == query.next()) {  //一行一行遍歷
3         //取出當前行的內容,以列為單位
4         qDebug() << query.value(0).toInt()  //取第一列
5                  << query.value(1).toString() //取第二列
6                  << query.value("age").toInt()
7                  << query.value("score").toInt();
8     }

完整代碼:

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QDebug>
 4 #include <QSqlDatabase>
 5 #include <QMessageBox>
 6 #include <QSqlError>
 7 #include <QSqlQuery>
 8 
 9 
10 Widget::Widget(QWidget *parent) :
11     QWidget(parent),
12     ui(new Ui::Widget)
13 {
14     ui->setupUi(this);
15 
16     //打印qt支持的數據庫驅動
17     qDebug() << QSqlDatabase::drivers();
18 
19     //添加sqlite數據庫
20     db = QSqlDatabase::addDatabase("QSQLITE");
21     //設置數據庫
22     db.setDatabaseName("../info.db");
23     //打開數據庫
24     if (db.open() == false) {
25         QMessageBox::warning(this, "錯誤", db.lastError().text());
26         return;
27     }
28     //操作sql語句
29     QSqlQuery query;
30     query.exec("create table student(id int primary key, name varchar(255), age int, score int);");
31     //批量插入:odbc風格
32     //預處理語句
33     query.prepare("insert into student(name, age, score) values(?, ?, ?);");
34     // 給字段設置內容
35     QVariantList nameList;
36     nameList << "xiaoming" << "xiaokong" << "xiaojiang";
37     QVariantList ageList;
38     ageList << 22 << 21 << 24;
39     QVariantList scoreList;
40     scoreList << 89 << 99 << 78;
41     //給字段綁定相應的值,必須按順序綁定
42     query.addBindValue(nameList);
43     query.addBindValue(ageList);
44     query.addBindValue(scoreList);
45     //執行預處理命令
46     query.execBatch();
47 
48     query.exec("select * from student");
49     while (true == query.next()) {  //一行一行遍歷
50         //取出當前行的內容,以列為單位
51         qDebug() << query.value(0).toInt()  //取第一列
52                  << query.value(1).toString() //取第二列
53                  << query.value("age").toInt()
54                  << query.value("score").toInt();
55     }
56 }
57 
58 Widget::~Widget()
59 {
60     delete ui;
61 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM