#include "widget.h"qsqlquery query(dbconn);//以下執行相關qsl語句
//新建student表,id設定為主鍵,還有乙個name項
query.exec("create table student(id varchar,name varchar)");
query.exec(qobject::tr("insert into student values(1,'李剛')"));
query.exec(qobject::tr("insert into student values(2,'蘋果')"));
query.exec(qobject::tr("insert into student values(3,'葡萄')"));
query.prepare("insert into student values(?,?)");
qvariantlist ids;
ids << 4 << 5 << 6;
query.addbindvalue(ids);
qvariantlist names;
names << "kevin" << "xiang" << "jie";
query.addbindvalue(names);
if( !query.execbatch() )
qdebug() << "error: " << query.lasterror();
query.exec("update student set name='xiangjie' where id=5"); //更新
query.exec("delete from student where id=6"); //刪除
"select id,name from student where id>=2");
query.exec("select * from student");
while(query.next())指向查詢到的第一條記錄,然後每次後移一條記錄
query.seek(3);
qsqlrecord record = query.record(); //獲取當前行記錄
qdebug() << "id: " << record.value("id").toint() << " name: " << record.value("name").tostring();
qsqlfield field = record.field(1); //獲取索引字段
qdebug() << "field name:" << field.name() << " field value:" << field.value().tostring();
query.exec(qobject::tr("drop table student")); //刪除table
//return a.exec();
}
Qt之運算元據庫(SQLite)
qtsql模組提供了與平台以及資料庫種類無關的訪問sql資料庫的介面,這個介面由利用qt的模型檢視結構將資料庫與使用者介面整合的一套類來支援。驅動程式 資料庫 qdb2 ibm db27.1版以及更新的版本 qibase borlandinterbase qmysql mysql qoci 甲骨文公...
Qt運算元據庫
qt運算元據庫其實不難,只要連線成功資料庫,寫對sql語句就行。連線資料庫可參照 簡單的在程式中寫死的sql語句寫法如下 qsqlquery query query.exec insert into tablename id,name values 001,test 先構造乙個qsqlquery的物...
QT運算元據庫
qsqldatabase db qsqldatabase adddatabase qmysql db.sethostname localhost db.setdatabasename test db.setusername root db.setpassword bool ok db.open qs...