Qt 鏁版嵁搴撳熀鏈娇鐢?-QSQLITE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt 鏁版嵁搴撳熀鏈娇鐢?-QSQLITE相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%8d%a0%e4%bd%8d%e7%ac%a6' title='鍗犱綅绗?>鍗犱綅绗?/a>   insert   return   drive   let   open()   art   rom   

include "mainwindow.h"

include

include

include

include

include

include

include

include

include

int main(int argc, char *argv[])
{
//杩炴帴鏁版嵁搴?/p>

QSqlDatabase db =QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("easybook-3313b0");          //璁剧疆鏁版嵁搴撲富鏈哄悕
db.setDatabaseName("qtDB.db");              //璁剧疆鏁版嵁搴撳悕
db.setUserName("ly");                //璁剧疆鏁版嵁搴撶敤鎴峰悕
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("鏁版嵁搴撳垱寤烘垚鍔燂紒
");
}
else
{
    qDebug()<<QObject::tr("error!
");
}
//鏌ヨ
query.exec("select * from automobil");//鎵цsql璇彞
QSqlRecord rec = query.record();//鑾峰彇杩斿洖缁撴灉
qDebug() << QObject::tr("automobil琛ㄥ瓧娈垫暟锛?quot; )<< 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();//鍦╭uery涓彃鍏ュ€?    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");//鎸塈D鎺掑簭
if(success)
    qDebug()<<QObject::tr("鎺掑簭 %1 鏉¤褰曪紝鑰楁椂锛?2 ms").arg(records).arg(t.elapsed());
else
    qDebug()<<QObject::tr("鎺掑簭澶辫触锛?quot;);

//鏇存柊璁板綍
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());

QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();

}



以上是关于Qt 鏁版嵁搴撳熀鏈娇鐢?-QSQLITE的主要内容,如果未能解决你的问题,请参考以下文章

鏁版嵁搴撳垎鐗?Sharding)

鏁版嵁搴撳涔犲綊绾虫€荤粨

杈炬ⅵ鏁版嵁搴撳洖椤惧強鍩烘湰浠嬬粛

鏁版嵁搴撳垎绫讳互鍙奛oSQL浠嬬粛

Oracle鏁版嵁搴撳父鐢ㄧ殑Sql璇彞鏁寸悊

drogon鍏ラ棬鏁欑▼涓?鎺у埗鍣ㄥ拰鏁版嵁搴撳鎴风浣跨敤