国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據庫

2019-11-08 20:07:30
字體:
來源:轉載
供稿:網友
//例子1#include <QCoreapplication>#include <QTextCodec>#include <QSqlDatabase>#include <QSqlQuery>#include <QTime>#include <QSqlError>#include <QtDebug>#include <QSqlDriver>#include <QSqlRecord>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE"); //使用QSQLITE驅動 db.setHostName("easybook-3313b0"); //設置數據庫主機名 db.setDatabaseName("qtDB.db"); //設置數據庫名 db.setUserName("zhouhejun"); //設置數據庫用戶名 db.setPassWord("123456"); //設置數據庫密碼 db.open(); //打開連接 //創建數據庫表 QSqlQuery query; bool success=query.exec("create table automobil(id int PRimary key,attribute varchar,type varchar,kind varchar,nation int,carnumber int,elevaltor int,distance int,oil int,temperature int)"); if(success) qDebug()<<QObject::tr("數據庫表創建成功!/n"); else qDebug()<<QObject::tr("數據庫表創建失敗!/n"); //查詢 query.exec("select * from automobil"); QSqlRecord rec = query.record(); //查詢返回的記錄 qDebug() << QObject::tr("automobil表字段數:" )<< rec.count(); //插入記錄 QTime t; t.start(); query.prepare("insert into automobil values(?,?,?,?,?,?,?,?,?,?)"); long records=100; for(int i=0;i<records;i++) { //設置對應位置的參數 query.bindValue(0,i); query.bindValue(1,"四輪"); query.bindValue(2,"轎車"); query.bindValue(3,"富康"); query.bindValue(4,rand()%100); query.bindValue(5,rand()%10000); query.bindValue(6,rand()%300); query.bindValue(7,rand()%200000); query.bindValue(8,rand()%52); query.bindValue(9,rand()%100); //執行插入語句 success=query.exec(); if(!success) { QSqlError lastError=query.lastError(); qDebug()<<lastError.driverText()<<QString(QObject::tr("插入失敗")); } } qDebug()<<QObject::tr("插入 %1 條記錄,耗時:%2 ms").arg(records).arg(t.elapsed()); //排序 t.restart(); success=query.exec("select * from automobil order by id desc"); if(success) qDebug()<<QObject::tr("排序 %1 條記錄,耗時:%2 ms").arg(records).arg(t.elapsed()); else qDebug()<<QObject::tr("排序失敗!"); //更新記錄 t.restart(); for(int i=0;i<records;i++) { query.clear(); query.prepare(QString("update automobil set attribute=?,type=?," "kind=?,nation=?," "carnumber=?,elevaltor=?," "distance=?,oil=?," "temperature=? where id=%1").arg(i)); query.bindValue(0,"四輪"); query.bindValue(1,"轎車"); query.bindValue(2,"富康"); query.bindValue(3,rand()%100); query.bindValue(4,rand()%10000); query.bindValue(5,rand()%300); query.bindValue(6,rand()%200000); query.bindValue(7,rand()%52); query.bindValue(8,rand()%100); success=query.exec(); if(!success) { QSqlError lastError=query.lastError(); qDebug()<<lastError.driverText()<<QString(QObject::tr("更新失敗")); } } qDebug()<<QObject::tr("更新 %1 條記錄,耗時:%2 ms").arg(records).arg(t.elapsed()); //刪除 t.restart(); query.exec("delete from automobil where id=15"); qDebug()<<QObject::tr("刪除一條記錄,耗時:%1 ms").arg(t.elapsed()); return 0; //return a.exec();}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏边| 陵川县| 芦溪县| 科尔| 石泉县| 建瓯市| 尼木县| 西平县| 洛南县| 湘潭市| 台中市| 诸暨市| 日照市| 千阳县| 托里县| 南京市| 怀安县| 静安区| 武义县| 罗山县| 东至县| 称多县| 新巴尔虎右旗| 察雅县| 眉山市| 富阳市| 乌拉特前旗| 罗江县| 壶关县| 蛟河市| 三明市| 荥阳市| 乌鲁木齐县| 东丰县| 兰西县| 额济纳旗| 九龙城区| 武宁县| 临高县| 武清区| 张北县|