#include "dbaccess.h"
#include <QSqlQuery>
#include <QSqlError>

dbaccess::dbaccess(QObject *parent) :
    QObject(parent)
{
    myDb = QSqlDatabase::addDatabase("QSQLITE");
    myDb.setDatabaseName("xy.sqlite");

    bool ok = myDb.open();
    if (! ok)
    {
        qCritical("Could not open database");
    }

    init();
}

void dbaccess::init()
{
    QSqlQuery query = myDb.exec("CREATE TABLE IF NOT EXISTS test ("
                                "  xid INTEGER PRIMARY KEY,"
                                "  ctext VARCHAR(100)"
                                ")");
}

void dbaccess::insert(QString xid, QString ctext)
{
    QSqlQuery query = myDb.exec("INSERT into test (xid, ctext) VALUES "
                                "(" + xid + ", '" + ctext + "')");
    QSqlError err = query.lastError();
    if (err.type() != QSqlError::NoError)
    {
        qCritical("error writing to database");
    }
}
