Seafood/populateDB/main.cpp
author John Kern <johnk@symbian.org>
Thu, 25 Mar 2010 13:55:27 -0700
changeset 5 98d749cef1a7
child 6 e8f500c881af
permissions -rwxr-xr-x
program to populate fish db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     1
#include <QtCore/QCoreApplication>
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     2
#include <QtSql>
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     3
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     4
#include "database.h"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     6
// This program is simply responsible for creating a SQLite database for the Seafood Selector
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     7
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     8
void insertFish(QString name, int cat, int calories,
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
     9
                float fat, float protein, float omega3,
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    10
                int cholesterol, int sodium)
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    11
{
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    12
    QSqlQuery query;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    13
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    14
    query.prepare("INSERT INTO fish (name, category, calories, fat, protein,omega3,cholesterol, sodium) "
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    15
                  "VALUES (:name,:category,:calories,:fat,:protein,:omega3,:cholesterol, :sodium) ");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    16
    query.bindValue(":name", name);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    17
    query.bindValue(":category",cat);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    18
    query.bindValue(":calories",calories);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    19
    query.bindValue(":fat",fat);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    20
    query.bindValue(":protein",protein);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    21
    query.bindValue(":omega3",omega3);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    22
    query.bindValue(":cholesterol",cholesterol);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    23
    query.bindValue(":sodium", sodium);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    24
    if (!query.exec())
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    25
    {
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    26
        qDebug() << query.lastError();
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    27
        qFatal("Failed to add fish.");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    28
    }
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    29
}
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    30
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    31
int main(int argc, char *argv[])
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    32
{
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    33
    QCoreApplication a(argc, argv);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    34
    QSqlDatabase db;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    35
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    36
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    37
    // Find QSLite driver
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    38
    db = QSqlDatabase::addDatabase("QSQLITE");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    39
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    40
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    41
    db.setDatabaseName("C:\\workspace\\qt\\populateDB\\seafood.db");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    42
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    43
    // Open databasee
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    44
    if(!db.open())
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    45
    {
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    46
        qDebug() << "DB: failed to open" << endl;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    47
        exit (1);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    48
    }
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    49
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    50
    qDebug() << "DB: database opened " << endl;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    51
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    52
    QSqlQuery query;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    53
    bool rc = true;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    54
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    55
   //  rc = query.exec("drop table if exists");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    56
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    57
    QString call("create table fish "
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    58
                 "(id integer AUTO INCREMENT primary key, "
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    59
                 "name varchar(32),"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    60
                 "category int,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    61
                 "calories int,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    62
                 "fat float,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    63
                 "protein float,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    64
                 "omega3 float,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    65
                 "cholesterol int,"
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    66
                 "sodium int)");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    67
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    68
    qDebug() << call << endl;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    69
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    70
    rc = query.exec(call);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    71
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    72
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    73
    insertFish("Crab, Dungeness",EBEST,86,0.96,17.4,0.3,59,295);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    74
    insertFish("Trout, rainbow (farmed)",EBEST,131,5.4,20.8,0.986,59,35);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    75
    insertFish("Cod, Pacific (trawl)",EOK,83,0.63,17.9,0.0,37,71);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    76
    insertFish("Tuna, canned light",EOK,103,1.01,22.0,0.256,47,37);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    77
    insertFish("Orange Roughy",EWORST,69,0.7,14.7,0.02,20,63);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    78
    insertFish("Salmon, farmed or Atlantic",EWORST,142,6.33,19.8,1.73,55,44);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    79
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    80
    return a.exec();
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    81
}