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 |
|
|
8 |
void insertFish(QString name, int cat, int calories,
|
|
9 |
float fat, float protein, float omega3,
|
|
10 |
int cholesterol, int sodium)
|
|
11 |
{
|
|
12 |
QSqlQuery query;
|
|
13 |
|
|
14 |
query.prepare("INSERT INTO fish (name, category, calories, fat, protein,omega3,cholesterol, sodium) "
|
|
15 |
"VALUES (:name,:category,:calories,:fat,:protein,:omega3,:cholesterol, :sodium) ");
|
|
16 |
query.bindValue(":name", name);
|
|
17 |
query.bindValue(":category",cat);
|
|
18 |
query.bindValue(":calories",calories);
|
|
19 |
query.bindValue(":fat",fat);
|
|
20 |
query.bindValue(":protein",protein);
|
|
21 |
query.bindValue(":omega3",omega3);
|
|
22 |
query.bindValue(":cholesterol",cholesterol);
|
|
23 |
query.bindValue(":sodium", sodium);
|
|
24 |
if (!query.exec())
|
|
25 |
{
|
|
26 |
qDebug() << query.lastError();
|
|
27 |
qFatal("Failed to add fish.");
|
|
28 |
}
|
|
29 |
}
|
|
30 |
|
|
31 |
int main(int argc, char *argv[])
|
|
32 |
{
|
|
33 |
QCoreApplication a(argc, argv);
|
|
34 |
QSqlDatabase db;
|
|
35 |
|
|
36 |
|
|
37 |
// Find QSLite driver
|
|
38 |
db = QSqlDatabase::addDatabase("QSQLITE");
|
|
39 |
|
|
40 |
|
|
41 |
db.setDatabaseName("C:\\workspace\\qt\\populateDB\\seafood.db");
|
|
42 |
|
|
43 |
// Open databasee
|
|
44 |
if(!db.open())
|
|
45 |
{
|
|
46 |
qDebug() << "DB: failed to open" << endl;
|
|
47 |
exit (1);
|
|
48 |
}
|
|
49 |
|
|
50 |
qDebug() << "DB: database opened " << endl;
|
|
51 |
|
|
52 |
QSqlQuery query;
|
|
53 |
bool rc = true;
|
|
54 |
|
|
55 |
// rc = query.exec("drop table if exists");
|
|
56 |
|
|
57 |
QString call("create table fish "
|
|
58 |
"(id integer AUTO INCREMENT primary key, "
|
|
59 |
"name varchar(32),"
|
|
60 |
"category int,"
|
|
61 |
"calories int,"
|
|
62 |
"fat float,"
|
|
63 |
"protein float,"
|
|
64 |
"omega3 float,"
|
|
65 |
"cholesterol int,"
|
|
66 |
"sodium int)");
|
|
67 |
|
|
68 |
qDebug() << call << endl;
|
|
69 |
|
|
70 |
rc = query.exec(call);
|
|
71 |
|
|
72 |
|
|
73 |
insertFish("Crab, Dungeness",EBEST,86,0.96,17.4,0.3,59,295);
|
|
74 |
insertFish("Trout, rainbow (farmed)",EBEST,131,5.4,20.8,0.986,59,35);
|
|
75 |
insertFish("Cod, Pacific (trawl)",EOK,83,0.63,17.9,0.0,37,71);
|
|
76 |
insertFish("Tuna, canned light",EOK,103,1.01,22.0,0.256,47,37);
|
|
77 |
insertFish("Orange Roughy",EWORST,69,0.7,14.7,0.02,20,63);
|
|
78 |
insertFish("Salmon, farmed or Atlantic",EWORST,142,6.33,19.8,1.73,55,44);
|
|
79 |
|
|
80 |
return a.exec();
|
|
81 |
}
|