Seafood/Fishes.cpp
changeset 14 a16afe3df8c9
parent 11 f3dbeee07821
child 15 0f80a0f39475
equal deleted inserted replaced
13:92f7e918c7b5 14:a16afe3df8c9
     6 #include "Fishes.h"
     6 #include "Fishes.h"
     7 
     7 
     8 Fishes::Fishes(QObject *parent) : QObject(parent)
     8 Fishes::Fishes(QObject *parent) : QObject(parent)
     9 {
     9 {
    10     QSqlDatabase db;
    10     QSqlDatabase db;
    11     this->dbErrString = "noErr";
       
    12 
    11 
    13     // Find QSLite driver
    12     // Find QSLite driver
    14     db = QSqlDatabase::addDatabase("QSQLITE");
    13     db = QSqlDatabase::addDatabase("QSQLITE");
    15 
    14 
    16 #ifdef Q_OS_SYMBIAN
    15 #ifdef Q_OS_SYMBIAN
    22     // Windows assumed.
    21     // Windows assumed.
    23     // unfortunately, "C:\Documents and Settings" is corrupted on my home PC. hard coding until I fix it. -jk
    22     // unfortunately, "C:\Documents and Settings" is corrupted on my home PC. hard coding until I fix it. -jk
    24     QString dbFile = "C:/workspace/QtExamples/Seafood/populateDB/seafood.db";
    23     QString dbFile = "C:/workspace/QtExamples/Seafood/populateDB/seafood.db";
    25 #endif
    24 #endif
    26     QFile f(dbFile);
    25     QFile f(dbFile);
    27     std::string errString(dbFile.toStdString());
       
    28     if (!f.exists()) {
    26     if (!f.exists()) {
    29         qWarning("db not found ");
    27         qWarning("db not found ");
    30     } else {
    28     } else {
    31         qWarning("found db ");
    29         qWarning("found db ");
    32     }
    30     }
    34     db.setDatabaseName(dbFile);
    32     db.setDatabaseName(dbFile);
    35 
    33 
    36     // Open databasee
    34     // Open databasee
    37     if(!db.open())
    35     if(!db.open())
    38     {
    36     {
    39         this->dbErrString =  db.lastError().databaseText();
       
    40 
       
    41         qWarning("DB: failed to open.");
    37         qWarning("DB: failed to open.");
    42 
    38 
    43         this->bestFish << "Abalone (farmed)" << "Anchovy, European" << "Barramundi U.S."
    39         this->bestFish << "Abalone (farmed)" << "Anchovy, European" << "Barramundi U.S."
    44             << "Capelin, smelt roe (Iceland)" << "Catfish (U.S.)" << "Clam, softshell"
    40             << "Capelin, smelt roe (Iceland)" << "Catfish (U.S.)" << "Clam, softshell"
    45             << "Clams (farmed)" << "Cod, AK (longline)" << "Crab, Dungeness"
    41             << "Clams (farmed)" << "Cod, AK (longline)" << "Crab, Dungeness"
    66  */
    62  */
    67 QString Fishes::getEcoDetails(QString name)
    63 QString Fishes::getEcoDetails(QString name)
    68 {
    64 {
    69     QString detailsInHtml;
    65     QString detailsInHtml;
    70     QSqlQuery query;
    66     QSqlQuery query;
    71     this->dbErrString = "noErr";
       
    72 
    67 
    73     query.prepare("select details from ecoDetails "
    68     query.prepare("select details from ecoDetails "
    74                   "where fid in (select fid from fish where name = :name )");
    69                   "where fid in (select fid from fish where name = :name )");
    75     query.bindValue(":name",name);
    70     query.bindValue(":name",name);
    76 
    71 
    77     if (!query.exec())
    72     if (!query.exec())
    78     {
    73     {
    79         QString errCode =  "failed to get eco details " + query.lastError().text();
    74         QString errCode =  "failed to get eco details " + query.lastError().text();
    80         qWarning(errCode.toStdString().c_str());
    75         qWarning(errCode.toStdString().c_str());
    81         this->dbErrString = name + " " + query.lastError().text();
       
    82     }
    76     }
    83 
    77 
    84     detailsInHtml.append("<html> <title>name</title> <body> <h2>Eco Details</h2> <ul> ");
    78     detailsInHtml.append("<html> <title>name</title> <body> <h2>Eco Details</h2> <ul> ");
    85     while (query.next()){
    79     while (query.next()){
    86         detailsInHtml.append( " <li>");
    80         detailsInHtml.append( " <li>");
    87         detailsInHtml.append( query.value(0).toString());
    81         detailsInHtml.append( query.value(0).toString());
    88         detailsInHtml.append( "</li> " );
    82         detailsInHtml.append( "</li> " );
    89     }
    83     }
    90     detailsInHtml.append("</ul> </body> </html> ");
    84     detailsInHtml.append("</ul> </body> </html> ");
    91     return  detailsInHtml;
    85     return  detailsInHtml;
       
    86 }
       
    87 
       
    88 void Fishes::getNutrition(QString name)
       
    89 {
       
    90     QSqlQuery query;
       
    91     QMap<QString, QString> nutrition;
       
    92 
       
    93     query.prepare("select calories,fat,protein,omega3,cholesterol,sodium from fish where name = :name ");
       
    94     query.bindValue(":name",name);
       
    95 
       
    96     if (!query.exec())
       
    97     {
       
    98         QString errCode =  "failed to get nutrition information " + query.lastError().text();
       
    99         qWarning(errCode.toStdString().c_str());
       
   100     }
       
   101 
       
   102     query.next();
       
   103     nutrition["Calories"] = query.value(0).toString();
       
   104     nutrition["Total Fat"] = query.value(1).toString();
       
   105     nutrition["Total Protein"] = query.value(2).toString();
       
   106     nutrition["Omega-3"] = query.value(3).toString();
       
   107     nutrition["Cholesterol"] = query.value(4).toString();
       
   108     nutrition["Sodium"] = query.value(5).toString();
       
   109 
       
   110     QMapIterator<QString, QString> i(nutrition);
       
   111     while (i.hasNext())
       
   112     {
       
   113          i.next();
       
   114          qDebug() << i.key() << ": " << i.value() << endl;
       
   115     }
    92 }
   116 }
    93 
   117 
    94 void Fishes::populate(TCATEGORIES cat)
   118 void Fishes::populate(TCATEGORIES cat)
    95 {
   119 {
    96     QSqlQuery query;
   120     QSqlQuery query;
   124     }
   148     }
   125 
   149 
   126 }
   150 }
   127 
   151 
   128 
   152 
   129 const QStringList Fishes::GetBest()
   153 const QStringList Fishes::getBest()
   130 {
   154 {
   131 
   155 
   132     return bestFish;
   156     return bestFish;
   133 }
   157 }
   134 
   158 
   135 const QStringList Fishes::GetOK()
   159 const QStringList Fishes::getOK()
   136 {
   160 {
   137     return okFish;
   161     return okFish;
   138 }
   162 }
   139 
   163 
   140 const QStringList Fishes::GetWorst()
   164 const QStringList Fishes::getWorst()
   141 {
   165 {
   142     return worstFish;
   166     return worstFish;
   143 }
   167 }