Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
#include "tietokanta.h"
#include <QtGui>
#include <QtSql>

Tietokanta::Tietokanta()
{

}

bool Tietokanta::LuoLiityntaTietokantaan()
{
    //Luodaan SqLite tietokanta
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Testidb.dat");
    if (!db.open())//avataan ja testataan aukesiko
    {//Naytetaan virhe, jos niin kavi
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        printf("DEBUG: connection NOT created\n");//printataan stdio liityntaan tulokset
        return false;
    }
    printf("DEBUG: connection created\n");
    return true;
}

void Tietokanta::LisaaRivi(
               QString      name,
               QString      force,
               QString      delta,
               QString      descr = "no comments"
               )
{
    QDateTime now = QDateTime::currentDateTime();
    QSqlQuery q;
    q.prepare("INSERT INTO tbl_data (name, force, delta,time,description) VALUES (?,?,?,?,?)");
    q.addBindValue(name);
    q.addBindValue(force);
    q.addBindValue(delta);
    q.addBindValue(now);
    q.addBindValue(descr);
    q.exec();
    q.clear();
}

void Tietokanta::TaytaDemoDataa()
{
    QSqlQuery query;
    for ( int i = 1,f=0 ; i < 100; i++, f++ )
    {
        QDateTime now = QDateTime::currentDateTime();
        QSqlQuery q;
        q.prepare("INSERT INTO tbl_data (name, force, delta,time,description) VALUES (?,?,?,?,?)");
        q.addBindValue("nimi ");
        q.addBindValue(i*2);
        q.addBindValue(f+1);
        q.addBindValue(now);
        q.addBindValue("descr ");
        q.exec();
        q.clear();
    }
}

void Tietokanta::LuoTietokantaTaulu(bool tuhotaan_jos_tosi)
{
    QSqlQuery q;
    if (tuhotaan_jos_tosi)
    {
        q.prepare("DROP TABLE tbl_data");
        q.exec();
    }
    q.clear();
    //luodaan sql-kaskyilla tietokantataulun sarakkeet
    q.prepare("CREATE TABLE tbl_data ("
                         "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                         "name VARCHAR(40) NOT NULL, "
                         "force DOUBLE NOT NULL, "
                         "delta DOUBLE NOT NULL, "
                         "time DATETIME, "
                         "description VARCHAR(80) NOT NULL)");
     q.exec();
}
Code Block

p, li { white-space: pre-wrap; }
#include <QtGui>
#include <QtSql>
#include "scooterwindow.h"



ScooterWindow::ScooterWindow()

{

    //luodaan malli sql.taululle

    model = new QSqlTableModel(this);

    //lisataan malliin taulu

    model->setTable("tbl_data");

    //laitetaan taulu nimen erusteella laskevaan jarjestykseen

    model->setSort(Scooter_Name, Qt::AscendingOrder);

    //laiteaan sarakkeille  otsikot

    model->setHeaderData(Scooter_Name, Qt::Horizontal, tr("Name"));

    model->setHeaderData(Scooter_Force, Qt::Horizontal, tr("Force"));

    model->setHeaderData(Scooter_Delta, Qt::Horizontal, tr("Delta"));

    model->setHeaderData(Scooter_Time, Qt::Horizontal, tr("Time"));

    model->setHeaderData(Scooter_Description, Qt::Horizontal,

                         tr("Description"));

    //otetaan malli kayttoon

    model->select();

    //tehdaan taululle nakyma

    view = new QTableView;

    //laitetaan malli nakymaan

    view->setModel(model);

    view->setSelectionMode(QAbstractItemView::SingleSelection);

    view->setSelectionBehavior(QAbstractItemView::SelectRows);

    view->setColumnHidden(Scooter_Id, true);

    view->resizeColumnsToContents();

    view->setEditTriggers(QAbstractItemView::NoEditTriggers);



    QHeaderView *header = view->horizontalHeader();

    header->setStretchLastSection(true);

    //tehdaan layout

    QHBoxLayout *mainLayout = new QHBoxLayout;

    //laitetaan layoout paanakymaan

    mainLayout->addWidget(view);

    setLayout(mainLayout);

    //annetaan paaikkunalle nimi

    setWindowTitle(tr("Testi applikaatio"));

}