5
|
1 |
#include <QtCore/QCoreApplication>
|
|
2 |
#include <QtSql>
|
|
3 |
|
|
4 |
#include "database.h"
|
|
5 |
|
|
6 |
// This program is simply responsible for creating a SQLite database for the Seafood Selector
|
|
7 |
|
6
|
8 |
void createTable(QString sqlStmt)
|
|
9 |
{
|
|
10 |
QSqlQuery query;
|
|
11 |
bool rc = true;
|
|
12 |
rc = query.exec(sqlStmt);
|
|
13 |
}
|
|
14 |
|
|
15 |
int getLastInsertRowId()
|
|
16 |
{
|
|
17 |
int rc = -1;
|
|
18 |
QSqlQuery query;
|
|
19 |
query.exec("select last_insert_rowid()");
|
|
20 |
if (!query.exec()) {
|
|
21 |
QString errCode = "last rowid query Failed: " + query.lastError().text();
|
|
22 |
qWarning(errCode.toStdString().c_str());
|
|
23 |
|
|
24 |
} else {
|
|
25 |
query.next();
|
|
26 |
rc = query.value(0).toInt();
|
|
27 |
}
|
|
28 |
return rc;
|
|
29 |
}
|
|
30 |
|
|
31 |
void insertFish( QString name, int cat, int calories,
|
5
|
32 |
float fat, float protein, float omega3,
|
|
33 |
int cholesterol, int sodium)
|
|
34 |
{
|
|
35 |
QSqlQuery query;
|
|
36 |
|
6
|
37 |
query.prepare("INSERT INTO fish ( name, category, calories, fat, protein,omega3,cholesterol, sodium) "
|
5
|
38 |
"VALUES (:name,:category,:calories,:fat,:protein,:omega3,:cholesterol, :sodium) ");
|
|
39 |
query.bindValue(":name", name);
|
|
40 |
query.bindValue(":category",cat);
|
|
41 |
query.bindValue(":calories",calories);
|
|
42 |
query.bindValue(":fat",fat);
|
|
43 |
query.bindValue(":protein",protein);
|
|
44 |
query.bindValue(":omega3",omega3);
|
|
45 |
query.bindValue(":cholesterol",cholesterol);
|
|
46 |
query.bindValue(":sodium", sodium);
|
|
47 |
if (!query.exec())
|
|
48 |
{
|
|
49 |
qDebug() << query.lastError();
|
|
50 |
qFatal("Failed to add fish.");
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
6
|
54 |
void insertEco(int lastID, QString details)
|
|
55 |
{
|
|
56 |
|
|
57 |
QSqlQuery query;
|
|
58 |
|
9
|
59 |
query.prepare("INSERT INTO ecoDetails (fid, details) VALUES (:fid,:details) ");
|
|
60 |
query.bindValue(":fid", lastID);
|
6
|
61 |
query.bindValue(":details",details);
|
|
62 |
if (!query.exec())
|
|
63 |
{
|
|
64 |
qDebug() << query.lastError();
|
|
65 |
qFatal("Failed to add eco detail.");
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
5
|
69 |
int main(int argc, char *argv[])
|
|
70 |
{
|
|
71 |
QCoreApplication a(argc, argv);
|
|
72 |
QSqlDatabase db;
|
|
73 |
|
|
74 |
// Find QSLite driver
|
|
75 |
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
76 |
|
6
|
77 |
db.setDatabaseName("C:\\workspace\\QtExamples\\Seafood\\populateDB\\seafood.db");
|
5
|
78 |
|
|
79 |
// Open databasee
|
|
80 |
if(!db.open())
|
|
81 |
{
|
|
82 |
qDebug() << "DB: failed to open" << endl;
|
|
83 |
exit (1);
|
|
84 |
}
|
|
85 |
|
|
86 |
qDebug() << "DB: database opened " << endl;
|
|
87 |
|
6
|
88 |
createTable("create table fish "
|
9
|
89 |
"(fid integer primary key, "
|
6
|
90 |
"name varchar(32),"
|
|
91 |
"category int,"
|
|
92 |
"calories int,"
|
|
93 |
"fat float,"
|
|
94 |
"protein float,"
|
|
95 |
"omega3 float,"
|
|
96 |
"cholesterol int,"
|
|
97 |
"sodium int)");
|
5
|
98 |
|
6
|
99 |
createTable ("create table ecoDetails (eid integer primary key, "
|
9
|
100 |
"fid integer, "
|
6
|
101 |
"details varchar(128))");
|
|
102 |
|
|
103 |
int lastID = -1;
|
|
104 |
insertFish("Crab, Dungeness",EBEST,86,0.96,17.4,0.3,59,295);
|
|
105 |
lastID = getLastInsertRowId();
|
|
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.");
|
|
107 |
insertEco(lastID, "Pots and traps are also equipped with built-in exits that allow escape of undersize crabs.");
|
|
108 |
insertEco(lastID, "This gear has negligible bycatch and causes little habitat damage.");
|
5
|
109 |
|
6
|
110 |
insertFish("Trout, rainbow (farmed)",EBEST,131,5.4,20.8,0.986,59,35);
|
|
111 |
lastID = getLastInsertRowId();
|
|
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.");
|
|
113 |
insertEco(lastID, "Industry practices and stringent government regulations also prevent pollution and fish escapes.");
|
5
|
114 |
|
6
|
115 |
insertFish("Cod, Pacific (trawl)",EOK,83,0.63,17.9,0.0,37,71);
|
|
116 |
lastID = getLastInsertRowId();
|
|
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.");
|
|
118 |
insertEco(lastID, "U.S. Pacific cod are also caught by bottom trawl, which can damage seafloor habitats.");
|
5
|
119 |
|
6
|
120 |
insertFish("Tuna, canned light",EOK,103,1.01,22.0,0.256,47,37);
|
|
121 |
lastID = getLastInsertRowId();
|
|
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.");
|
|
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).");
|
|
124 |
insertEco(lastID, "This highly migratory species is managed by international organizations.");
|
5
|
125 |
|
6
|
126 |
insertFish( "Orange Roughy",EWORST,69,0.7,14.7,0.02,20,63);
|
|
127 |
lastID = getLastInsertRowId();
|
|
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.");
|
|
129 |
insertEco(lastID, "Management in New Zealand and Australia is good, but unregulated landings by other countries continue.");
|
|
130 |
|
5
|
131 |
insertFish("Salmon, farmed or Atlantic",EWORST,142,6.33,19.8,1.73,55,44);
|
6
|
132 |
lastID = getLastInsertRowId();
|
|
133 |
insertEco(lastID, "Atlantic salmon are usually farmed in large-scale, densely stocked netpens that pollute surrounding waters with waste and chemicals.");
|
|
134 |
insertEco(lastID, "Fish can escape and compete for resources, breed with or spread parasites to wild fish.");
|
5
|
135 |
|
6
|
136 |
return 0;
|
5
|
137 |
}
|