QT入門(十二) 資料庫的基本操作

2021-10-24 02:35:00 字數 2661 閱讀 8379

資料表的增加

**的查詢

根據輸入的條件查詢

表的刪除

修改顯示所有註冊使用者的資訊

新建表,增刪改查 登入 註冊 刪除 查詢 顯示資料庫的表

關於增刪改查的sql語句在寫**的時候,一點點都不能寫錯!!!sql語句寫錯了編譯不會報錯,但是執行就會有問題

//---------------表的標頭檔案

#include

#include

#include

#include

#include

widget::widget(qwidget *parent)

: qwidget(parent),

ui(new ui::widget)

//建立**

//先定義 訪問的類

qsqlquery query;

//建立乙個叫user的表,一共有三個字段

//(id ---------主鍵 自動增加(primary key),username----賬號 不可以重複也不能為空(unique not null),password------密碼(不為空))

//引號裡面是sql語句的固定格式 user是自定義的表名

//integer 整數

//ntext 字串

//unique not null 唯一並且不為空

//exec返回值為布林型,建立成功為true

if(!query.exec(

"create table if not exists user(id integer primary key autoincrement,username ntext unique not null,password ntext not null)"))}

widget::~widget(

)註冊使用者

//註冊  **的插入操作

void widget::on_pushbutton_clicked(

)else

}

//登入  **的查詢

void widget::on_pushbutton_2_clicked(

)else

}

//查詢

void widget::on_pushbutton_4_clicked(

)else

}

//刪除

void widget::on_pushbutton_3_clicked(

)else

}

//修改賬號

void widget::on_pushbutton_6_clicked(

)else

}

//顯示所有註冊使用者的資訊

void widget::on_pushbutton_5_clicked(

)// //初始化列數(mysql中存在乙個information_schema這個資料庫,存放各個表的資訊)

// ui->tablewidget->setcolumncount(2)

;//列

// ui->tablewidget->sethorizontalheaderlabels(qstringlist(

)<<

"id"

<<

"username"

); //設定行頭

// qstring sql1 = qstring(

"select * from user");

// qsqlquery query1(sql1)

;//自動執行

// for(int i =0;query1.next(

);i++)

//

static qsqlquerymodel *mode= new qsqlquerymodel(ui->tableview)

; mode->setquery(

"select * from user");

mode->setheaderdata(0,qt::horizontal,tr(

"id"

));//設定表頭

mode->setheaderdata(1, qt::horizontal, tr(

"username"));

ui->tableview->setmodel(mode)

;}

涉及到乙個資料模型

注釋掉的內容是另一種顯示方法

Qt 資料庫操作

sql 是運算元據庫的標準語言,適用於mysql oracle db2 等資料庫。qt 的 qtsql 模組基於 sql 提供了相關資料庫操作函式,因此得以使這些資料庫操作起來大同小異。下面的以sqlite資料庫為例記錄,生成的資料庫可通過sqlitestudio進行視覺化操作。在.pro檔案中新增...

QT資料庫操作

瀏覽 在qsqlquery類中當執行exec 後會把指標放在記錄集中第乙個記錄之上,所以需要呼叫qsqlquery next 來獲取第乙個資料,下面通過迴圈體來遍歷所有表中的資料。while query.next qsqlquery value 函式得到當前記錄區域中的資料,qsqlquery va...

oracle資料庫入門pl sql基本操作

create table tablename id number not null primary key,name varchar 20 create time date,total consumption number 4,2 insert into tablename values 222,s...