Contents |
SqLite allows you to store data in a file and query this data using SQL just as you would do with a database server. As an example if you want to manage your stock transactions selecting all buy transactions from a flat file is tedious. Using sqlite you can just issue something like
select * from transactions where type='buy'
and you do not need a database server.
#include <QtSql>
int main(int argc, char *argv[])
{
QSqlDatabase db;
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sqlite.dat");
if (db.open())
qDebug() << "success";
else
qDebug() << "failed";
QSqlQuery query;
query.exec("create table stocks(id int primary key, name varchar(20))");
db.commit();
db.close();
}
QT += core gui sql
TARGET = sqlite
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
To build the example project, use the command
qmake && make -j4
To run the example project, first delete sqlite.dat from your home directory. We do this to show that our example program creates it.
rm ~/sqlite.dat
Then run our example program:
./sqlite
And you will find the database file in your home folder:
ll /root/sqlite.dat -rw-r--r-- 1 root root 3072 Nov 10 10:37 /root/sqlite.dat
http://doc.trolltech.com/4.5/sql-relationaltablemodel.html http://commits.kde.org/kdeexamples/23d2202b0e7aca8999928374746da5733712ebc8