Difference between revisions of "Development/Tutorials/SqLite"

Jump to: navigation, search
(Analyze it)
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
= Overview =
 +
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.
 +
 +
= Files =
 +
 +
== main.cpp ==
 
<pre>
 
<pre>
 
#include <QtSql>
 
#include <QtSql>
Line 19: Line 27:
 
</pre>
 
</pre>
  
 +
== sqlite.pro ==
 
<pre>
 
<pre>
 
QT      += core gui sql
 
QT      += core gui sql
Line 26: Line 35:
  
  
SOURCES += main.cpp\
+
SOURCES += main.cpp
        mainwindow.cpp
+
  
HEADERS  += mainwindow.h
 
 
FORMS    += mainwindow.ui
 
 
</pre>
 
</pre>
 +
 +
= Build it =
 +
To build the example project, use the command
 +
qmake && make -j4
 +
 +
= Run it =
 +
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
 +
 +
= Analyze it =
 +
You can now use the command sqlite3 to analyze your database file:
 +
# sqlite3 sqlite.dat
 +
SQLite version 3.7.8 2011-09-19 14:49:19
 +
Enter ".help" for instructions
 +
Enter SQL statements terminated with a ";"
 +
sqlite> .schema stocks
 +
CREATE TABLE stocks(id int primary key, name varchar(20));
 +
sqlite> .exit
  
 
= See also =
 
= See also =
http://doc.trolltech.com/4.5/sql-relationaltablemodel.html
+
* [http://developer.qt.nokia.com/doc/qt-4.8/qsqlquery.html QSQLQuery class documentation]
http://commits.kde.org/kdeexamples/23d2202b0e7aca8999928374746da5733712ebc8
+
* http://doc.trolltech.com/4.5/sql-relationaltablemodel.html
 +
* http://commits.kde.org/kdeexamples/23d2202b0e7aca8999928374746da5733712ebc8

Latest revision as of 14:34, 17 December 2011

Contents

[edit] Overview

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.

[edit] Files

[edit] main.cpp

#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();
}

[edit] sqlite.pro

QT       += core gui sql

TARGET = sqlite
TEMPLATE = app


SOURCES += main.cpp

[edit] Build it

To build the example project, use the command

qmake && make -j4

[edit] Run it

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

[edit] Analyze it

You can now use the command sqlite3 to analyze your database file:

# sqlite3 sqlite.dat 
SQLite version 3.7.8 2011-09-19 14:49:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema stocks
CREATE TABLE stocks(id int primary key, name varchar(20));
sqlite> .exit

[edit] See also


This page was last modified on 17 December 2011, at 14:34. This page has been accessed 5,086 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal