您好,登錄后才能下訂單哦!
今天小編給大家分享一下Qt怎么創建SQlite數據庫的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
Qt 創建 SQlite數據庫
void Widget::initDB() { // 創建并打開數據庫 QSqlDatabase database; database = QSqlDatabase::addDatabase("QSQLITE"); // qDebug() << QApplication::applicationDirPath(); // 獲取應用程序當前目錄 database.setDatabaseName("test.sqlite3"); if(!database.open()) { qDebug() << "Error: Failed to connect database." << database.lastError(); } else { qDebug() << "Succeed to connect database."; } // 創建表格 QSqlQuery sql_query = database.exec("DROP TABLE student"); // 先清空一下表,可按需添加此句 // sql_query.exec("DROP TABLE student"); // 創建表格student if(!sql_query.exec("create table student(UserId int primary key, UserName text, PassWord text)")) { qDebug() << "Error: Fail to create table." << sql_query.lastError(); } else { qDebug() << "Table created!"; } // 填充表 if(!sql_query.exec("INSERT INTO student VALUES(1, 'AppleCai', '23')")) { qDebug() << "Error: Fail to create table." << sql_query.lastError(); } else { qDebug() << "add one created!"; } // 批量填充表 QStringList names; names << "小A" << "小B" << "小C" << "小D" << "小E" << "小F" << "小G" << "小H" << "小I"; QStringList password; password << "12" << "23" << "34" << "45" << "56" << "67" << "78" << "89" << "90"; // 綁定關鍵字后才能進行操作 sql_query.prepare("INSERT INTO student (UserId, UserName, PassWord) " "VALUES (:UserId, :UserName, :PassWord)"); qint8 i = 0; foreach (QString name, names) // 從names表里獲取每個名字 { sql_query.bindValue(":UserId", i+2); // 向綁定值里加入名字 sql_query.bindValue(":UserName", name); // 成績 sql_query.bindValue(":PassWord", password[i]); // 班級 if(!sql_query.exec()) { qDebug() << "Error: Fail." << sql_query.lastError(); } i++; } // 讀取sqlite studentInfo tmp; QVector<studentInfo> infoVect; // 數據庫緩存 sql_query.exec("SELECT * FROM student WHERE UserId >= 5 AND UserId <= 9;"); while (sql_query.next()) { tmp.UserId = sql_query.value(0).toInt(); tmp.UserName = sql_query.value(1).toString(); tmp.Password = sql_query.value(2).toString(); qDebug() << tmp.UserId << tmp.UserName << tmp.Password; infoVect.push_back(tmp); } qDebug("done"); // 更改表中數據 sql_query.prepare("UPDATE student SET PassWord = 'admin' WHERE UserName = 'AppleCai'"); if(!sql_query.exec()) { qDebug() << "Error: Fail." << sql_query.lastError(); } // 刪除表中數據 sql_query.prepare("DELETE FROM student WHERE UserName = '小H'"); if(!sql_query.exec()) { qDebug() << "Error: Fail." << sql_query.lastError(); } }
以下是個人代碼備份
這個代碼是在qt寫的,包含了數據庫的創建和寫入,但是我在項目準備直接在dataGrip把數據一鍵導入創建好數據庫之后再用qt里面的sql語句讀,所以就不需要這一部分了
#include "sqlitedatabase.h" SqliteDatabase::SqliteDatabase() { qDebug() << "hhh"; // initPickNameDB(); } void SqliteDatabase::initPickNameDB() { // 創建并打開數據庫 QSqlDatabase database; database = QSqlDatabase::addDatabase("QSQLITE"); // qDebug() << QApplication::applicationDirPath(); database.setDatabaseName(QApplication::applicationDirPath() + "/CONFIG/" + "PickNameDB.sqlite3"); if(!database.open()) { qDebug() << "Error: Failed to connect database." << database.lastError(); } else { qDebug() << "Succeed to connect database."; } // 創建表格 先清空一下表 QSqlQuery sql_query = database.exec("DROP TABLE department"); sql_query = database.exec("DROP TABLE person"); if(!sql_query.exec("create table department (Id int primary key not null, " "DeptName vchar(50) not null )")) { qDebug() << "Error: Fail to create department table." << sql_query.lastError(); } else { qDebug() << "Department table created!"; } if(!sql_query.exec("create table person (Id int primary key not null , " "DeptID integer not null , " "PerName vchar(50) not null, " "foreign key(DeptID) references department (Id))")) { qDebug() << "Error: Fail to create person table." << sql_query.lastError(); } // 填充表 // sql_query.exec("insert into department (id, name) values (1, '辦領導')"); // sql_query.exec("insert into department (id, name) values (2, '綜合處')"); // sql_query.exec("insert into department (id, name) values (3, '政策法規處')"); // sql_query.exec("insert into department (id, name) values (4, '機構改革處')"); // sql_query.exec("insert into department (id, name) values (5, '黨群政法行政機構編制管理處')"); // sql_query.exec("insert into department (id, name) values (6, '政府行政機構編制管理處')"); // sql_query.exec("insert into department (id, name) values (7, '市縣行政機構編制管理處')"); // sql_query.exec("insert into department (id, name) values (8, '事業機構編制管理處')"); // sql_query.exec("insert into department (id, name) values (9, '事業單位登記管理處')"); // sql_query.exec("insert into department (id, name) values (10, '機構編制監督檢查處')"); // sql_query.exec("insert into department (id, name) values (11, '人事處')"); // sql_query.exec("insert into department (id, name) values (12, '機關黨委')"); // sql_query.exec("insert into department (id, name) values (13, '省機構編制電子政務中心')"); // sql_query.exec("insert into department (id, name) values (14, '省機構編制研究中心')"); // 批量填充表 QStringList deptNames; deptNames << "辦領導" << "綜合處" << "政策法規處" << "機構改革處" << "黨群政法行政機構編制管理處" << "政府行政機構編制管理處" << "市縣行政機構編制管理處" << "事業機構編制管理處" << "事業單位登記管理處" << "機構編制監督檢查處" << "人事處" << "機關黨委" << "省機構編制電子政務中心" << "省機構編制研究中心"; // 綁定關鍵字后才能進行操作 sql_query.prepare("INSERT INTO department (Id, DeptName) " "VALUES (:Id, :DeptName)"); qint8 i = 0; foreach (QString deptName, deptNames) { sql_query.bindValue(":Id", i + 1); sql_query.bindValue(":DeptName", deptName); if(!sql_query.exec()) { qDebug() << "Error: Fail." << sql_query.lastError(); } i++; } // 讀取sqlite department dept; QVector<department> tmpDept; // 數據庫緩存 sql_query.exec("SELECT * FROM "); }
以上就是“Qt怎么創建SQlite數據庫”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。