瀏覽:
在qsqlquery類中當執行exec()後會把指標放在記錄集中第乙個記錄之上,所以需要呼叫qsqlquery::next()來獲取第乙個資料,下面通過迴圈體來遍歷所有表中的資料。
while(query.next())
qsqlquery::value()函式得到當前記錄區域中的資料,qsqlquery::value()預設返回的是乙個qvariant型別,qt提供了幾種可遠的型別支援,它們是c++的基本的型別,如:int,qstring,qbytearray。對於不同型別的轉換需要使用qt提供的函式來實現,例如qvariant::tostring()與qvaniant::toint().
刪除:
qsqlquery query;bool
value;
value = query.exec(「delete
from book where id = 「『+id+」』」); if
(value)
qmessagebox::information(
this,"
notice
","add sucessful!");
else
qmessagebox::information(
this,"
notice
","add failed!
");
修改:
qsqlquery query;bool
value;
value = query.exec(「update book name set name=「『+name+'
"」);
if(value)
qmessagebox::information(
this,"
notice
","add sucessful!");
else
qmessagebox::information(
this,"
notice
","add failed!
");
使用了prepare()函式:
query.prepare("insert into student(id, name) values (?, ?)");
query.bindvalue(
0, 5
);query.bindvalue(
1, "
sixth");
query.exec();
也可以利用addbindvalue()函式,這樣就可以省去編號,它是按順序給屬性賦值的,如下:
query.prepare(
"insert into student(id, name) values (?, ?)");
query.addbindvalue(5);
query.addbindvalue(
"sixth");
query.exec();
qsqllite好像不支援size函式,在stackoverflow上看到乙個方法可以達到效果:
qsqlquery q;q.exec(
"select * from table");
q.last();
qdebug()
<< q.at() + 1;
Qt 資料庫操作
sql 是運算元據庫的標準語言,適用於mysql oracle db2 等資料庫。qt 的 qtsql 模組基於 sql 提供了相關資料庫操作函式,因此得以使這些資料庫操作起來大同小異。下面的以sqlite資料庫為例記錄,生成的資料庫可通過sqlitestudio進行視覺化操作。在.pro檔案中新增...
qt mysql 操作 QT資料庫操作
瀏覽 在qsqlquery類中當執行exec 後會把指標放在記錄集中第乙個記錄之上,所以需要呼叫qsqlquery next 來獲取第乙個資料,下面通過迴圈體來遍歷所有表中的資料。while query.next qstring name query.value 0 tostring qdebug ...
Qt操作Access資料庫
qt為資料庫訪問提供qtsql模組實現了資料庫與qt應用程式的無縫整合。qtsql模組使用驅動外掛程式與不同的資料庫介面通訊。qt自帶有qodbc驅動,通過windows平台上提供的odbc驅動訪問支援odbc的資料庫,如ms access sql server等 windows xp 自帶有acc...