Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
19
 
286 werner 20
#include "sqlhelper.h"
21
#include <QtSql>
22
 
697 werner 23
 
24
/** @class SqlHelper
25
  @ingroup tools
26
  A helper class for simple execution of database commands.
27
*/
286 werner 28
SqlHelper::SqlHelper()
29
{
30
}
31
 
32
/** execute 'query' against database 'database'. The first column of the first row are returned.
33
  A Null-Variant is returned, if the query has no results. */
34
QVariant SqlHelper::queryValue(const QString &query, const QSqlDatabase &database)
35
{
36
    QSqlQuery q(database);
37
    if (!q.exec(query)) {
38
        qDebug() << "query"<< query << " raised SQL-Error:" << q.lastError().text();
39
        return QVariant();
40
    }
41
    if (q.next()) {
42
        return q.value(0);
43
    }
44
    return QVariant();
45
}
46
 
287 werner 47
/** execute 'query' against database 'database'.
48
Use for insert, update, ... statements without return values. */
49
bool SqlHelper::executeSql(const QString &query, const QSqlDatabase &database)
286 werner 50
{
51
    QSqlQuery q(database);
52
    bool success = q.exec(query);
53
    if (!success)
54
        qDebug() << "query"<< query << " raised SQL-Error:" << q.lastError().text();
55
    return success;
56
}