qtリンクsqliteデータベース


qtでよく使われるデータベースにはmysqlとsqliteがあり、いずれも軽量級の関係型のデータベースであり、使用と操作も非常に便利で、以下にqtリンクmysqlとsqliteデータベースの操作手順について簡単な記録を行う.
1、qtリンクsqliteデータベース
1)        
                。
#include <QSqlQuery>
#include <QSqlError>
#include <QtSql>
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  
2)          
db.setDatabaseName("people.db"); 
4、          
      :db.open()
         ,         :db.lastError().text()
     ,              :
    if (!db.open())  
    {  
        qDebug()<<"open database failed ---"<<db.lastError().text()<<"/n";  
        return -1;  
    }
5、     (        )
QSqlQuery query;  
    bool ok = query.exec(
        "CREATE TABLE IF NOT EXISTS people ( id INTEGER PRIMARY KEY AUTOINCREMENT,"  
         "name VARCHAR(20) NOT NULL,"  
         "age INTEGER NULL);"
     ); 
    if (ok)  
    {  
        qDebug()<<"ceate table partition success/n";  
    }  
    else  
    {  
        qDebug()<<"ceate table partition failed/n";  
    }  
    for (int i = 0; i< 3; ++i)  
    {  
        query.prepare("INSERT INTO people (id, name, age) VALUES (:id, :name, :age)");  
        query.bindValue(":name", QString("smith_%1").arg(i+1));  
        query.bindValue(":age", 20+i*5);  
        query.exec();  
    }  
  query.exec("SELECT id, name, age FROM people");  
    while (query.next())  
    {  
        qDebug()<<"people("<<query.value(0).toInt()<<") name:"<<query.value(1).toString()<<" age:"<<query.value(2).toInt();  
    }  
    return a.exec();  
}  

以上がqt操作sqliteデータベースの具体的な操作手順である、