Seafood/populateDB/main.cpp
author John Kern <johnk@symbian.org>
Thu, 09 Sep 2010 20:03:34 -0700
changeset 47 c6e75ec74fc8
parent 9 23f6727b5587
permissions -rwxr-xr-x
add colors via delegate
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
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
     8
void createTable(QString sqlStmt)
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
     9
{
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    10
    QSqlQuery query;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    11
    bool rc = true;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    12
    rc = query.exec(sqlStmt);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    13
}
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    14
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    15
int getLastInsertRowId()
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    16
{
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    17
    int rc = -1;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    18
    QSqlQuery query;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    19
    query.exec("select last_insert_rowid()");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    20
    if (!query.exec()) {
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    21
        QString errCode =  "last rowid query Failed: " + query.lastError().text();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    22
        qWarning(errCode.toStdString().c_str());
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    23
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    24
    } else {
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    25
        query.next();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    26
        rc = query.value(0).toInt();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    27
    }
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    28
    return rc;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    29
}
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    30
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    31
void insertFish( QString name, int cat, int calories,
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    32
                float fat, float protein, float omega3,
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    33
                int cholesterol, int sodium)
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    34
{
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    35
    QSqlQuery query;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    36
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    37
    query.prepare("INSERT INTO fish ( name, category, calories, fat, protein,omega3,cholesterol, sodium) "
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    38
                  "VALUES (:name,:category,:calories,:fat,:protein,:omega3,:cholesterol, :sodium) ");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    39
    query.bindValue(":name", name);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    40
    query.bindValue(":category",cat);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    41
    query.bindValue(":calories",calories);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    42
    query.bindValue(":fat",fat);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    43
    query.bindValue(":protein",protein);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    44
    query.bindValue(":omega3",omega3);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    45
    query.bindValue(":cholesterol",cholesterol);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    46
    query.bindValue(":sodium", sodium);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    47
    if (!query.exec())
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
        qDebug() << query.lastError();
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    50
        qFatal("Failed to add fish.");
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
}
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    53
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    54
void insertEco(int lastID, QString details)
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    55
{
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    56
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    57
    QSqlQuery query;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    58
9
23f6727b5587 fixed typo
User@User-PC.domain_not_set.invalid
parents: 6
diff changeset
    59
    query.prepare("INSERT INTO ecoDetails (fid, details) VALUES (:fid,:details) ");
23f6727b5587 fixed typo
User@User-PC.domain_not_set.invalid
parents: 6
diff changeset
    60
    query.bindValue(":fid", lastID);
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    61
    query.bindValue(":details",details);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    62
    if (!query.exec())
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    63
    {
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    64
        qDebug() << query.lastError();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    65
        qFatal("Failed to add eco detail.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    66
    }
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    67
}
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    68
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    69
int main(int argc, char *argv[])
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    70
{
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    71
    QCoreApplication a(argc, argv);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    72
    QSqlDatabase db;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    73
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    74
    // Find QSLite driver
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    75
    db = QSqlDatabase::addDatabase("QSQLITE");
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    76
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    77
    db.setDatabaseName("C:\\workspace\\QtExamples\\Seafood\\populateDB\\seafood.db");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    78
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    79
    // Open databasee
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    80
    if(!db.open())
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    81
    {
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    82
        qDebug() << "DB: failed to open" << endl;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    83
        exit (1);
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    84
    }
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    85
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    86
    qDebug() << "DB: database opened " << endl;
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    87
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    88
    createTable("create table fish "
9
23f6727b5587 fixed typo
User@User-PC.domain_not_set.invalid
parents: 6
diff changeset
    89
                "(fid integer primary key, "
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    90
                "name varchar(32),"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    91
                "category int,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    92
                "calories int,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    93
                "fat float,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    94
                "protein float,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    95
                "omega3 float,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    96
                "cholesterol int,"
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    97
                "sodium int)");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
    98
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
    99
    createTable ("create table ecoDetails (eid integer primary key, "
9
23f6727b5587 fixed typo
User@User-PC.domain_not_set.invalid
parents: 6
diff changeset
   100
                 "fid integer, "
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   101
                 "details varchar(128))");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   102
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   103
    int lastID = -1;
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   104
    insertFish("Crab, Dungeness",EBEST,86,0.96,17.4,0.3,59,295);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   105
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   106
    insertEco(lastID,"Only adult males are caught in this fishery, which uses pots and traps made of biodegradable webs to avoid \'ghost fishing\' from lost gear.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   107
    insertEco(lastID, "Pots and traps are also equipped with built-in exits that allow escape of undersize crabs.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   108
    insertEco(lastID, "This gear has negligible bycatch and causes little habitat damage.");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   109
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   110
    insertFish("Trout, rainbow (farmed)",EBEST,131,5.4,20.8,0.986,59,35);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   111
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   112
    insertEco(lastID, "Although trout consume considerable amounts of wild fish in their feed, recent improvements have made them less reliant on this finite natural resource.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   113
    insertEco(lastID, "Industry practices and stringent government regulations also prevent pollution and fish escapes.");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   114
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   115
    insertFish("Cod, Pacific (trawl)",EOK,83,0.63,17.9,0.0,37,71);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   116
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   117
    insertEco(lastID, "U.S. Pacific cod populations are healthy, and cod caught by bottom longline, traps or hook-and-line do the least environmental harm.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   118
    insertEco(lastID, "U.S. Pacific cod are also caught by bottom trawl, which can damage seafloor habitats.");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   119
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   120
    insertFish("Tuna, canned light",EOK,103,1.01,22.0,0.256,47,37);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   121
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   122
    insertEco(lastID, "Canned light tuna consists primarily of skipjack. These small tunas are resilient to fishing pressure because of their short life spans and rapid growth and reproduction rates.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   123
    insertEco(lastID, "Most skipjack are caught by pole-and-line (a method of fishing that does relatively little ecological harm) or purse seine fleets (which can result in considerable bycatch).");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   124
    insertEco(lastID, "This highly migratory species is managed by international organizations.");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   125
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   126
    insertFish( "Orange Roughy",EWORST,69,0.7,14.7,0.02,20,63);
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   127
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   128
    insertEco(lastID, "Orange roughy mature at a very slow rate and live extremely long lives. Intensive exploitation in past decades has greatly decreased populations.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   129
    insertEco(lastID, "Management in New Zealand and Australia is good, but unregulated landings by other countries continue.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   130
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   131
    insertFish("Salmon, farmed or Atlantic",EWORST,142,6.33,19.8,1.73,55,44);
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   132
    lastID = getLastInsertRowId();
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   133
    insertEco(lastID, "Atlantic salmon are usually farmed in large-scale, densely stocked netpens that pollute surrounding waters with waste and chemicals.");
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   134
    insertEco(lastID, "Fish can escape and compete for resources, breed with or spread parasites to wild fish.");
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   135
6
e8f500c881af add content to eco details
John Kern <johnk@symbian.org>
parents: 5
diff changeset
   136
    return 0;
5
98d749cef1a7 program to populate fish db
John Kern <johnk@symbian.org>
parents:
diff changeset
   137
}