1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright 2001-2004 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*
This file illustrates how to execute some operations relative to attribute tables
in a MySQL TerraLib database:
- how to create an attribute table;
- how to add some columns;
- how to insert same data into it;
- how to retrieve some rows using an SQL.
Author: Lubia Vinhas
*/
#include <TeTable.h>
#include <TeMySQL.h>
int main()
{
// Datatabase server parameters
string host = "localhost";
string dbname = "DB320RC1";
string user = "root";
string password = "vinhas";
// Open a connection to the DB320RC1 MySQL database
TeDatabase* db = new TeMySQL();
if (!db->connect(host, user, password, dbname))
{
cout << "Error: " << db->errorMessage() << endl;
cout << endl << "Press Enter\n";
getchar();
return 1;
}
cout << "Connection successful to the database \"" << dbname << "\" on MySQL server \"" << host << "\" !\n";;
// Create a table called "TesteTable"
string tableName = "TesteTable";
if (db->tableExist(tableName))
{
cout << "There is already a table named \"TesteTable\" in the TerraLib database!\n";
cout << "Some data will be retrieved from the table already created:\n\n";
}
else
{
// 1) Create a table
TeAttributeList attList;
TeAttribute at;
at.rep_.name_= "IntCol";
at.rep_.type_ = TeINT;
at.rep_.isPrimaryKey_ = true;
attList.push_back(at);
if (!db->createTable(tableName, attList))
{
cout << "Fail to create the table: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
//2) Add new columns to the table
TeAttribute atr;
atr.rep_.name_= "TimeCol";
atr.rep_.type_ = TeDATETIME;
atr.dateTimeFormat_ = "DDsMMsYYYYsHHsmmsSS";
atr.dateChronon_ = TeSECOND;
atr.timeSeparator_ = ":";
atr.dateSeparator_ = "/";
attList.push_back(atr);
if (!db->addColumn(tableName, atr.rep_))
{
cout << "Fail to add the column \"TimeCol\" to the table: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
atr.rep_.name_= "RealCol";
atr.rep_.type_ = TeREAL;
atr.rep_.numChar_ = 15;
atr.rep_.decimals_ = 4;
attList.push_back(atr);
if (!db->addColumn(tableName, atr.rep_))
{
cout << "Fail to add the column \"RealCol\" to the table: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
atr.rep_.name_= "StringCol";
atr.rep_.type_ = TeSTRING;
atr.rep_.numChar_ = 10;
attList.push_back(atr);
if (!db->addColumn(tableName, atr.rep_))
{
cout << "Fail to add the column \"StringCol\" to the table: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
//3) Add two new rows to the table
TeTable table(tableName, attList, ""); // creates a table in memory
TeTableRow row1; // fill some rows
row1.push_back("1");
row1.push_back("25/4/2003 07:14");
row1.push_back("15.23");
row1.push_back("object1");
table.add(row1);
TeTableRow row2;
row2.push_back("2");
row2.push_back("01/03/1975 21:30:15");
row2.push_back("56.87");
row2.push_back("object2");
table.add(row2);
if (!db->insertTable(table)) // save the table into the database
{
cout << "Fail to save the table: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
}
// 4) Retrieve some data from the table using a SQL query
TeDatabasePortal* portal = db->getPortal();
if (!portal)
{
cout << "Fail to get a portal fom the database: " << db->errorMessage() << endl;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
string sql = "SELECT * FROM " + tableName;
if (portal->query(sql) == false)
{
cout << "Fail to submit the query: " << db->errorMessage() << endl;
delete portal;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 1;
}
// Retrieve the attributes from the table
int count = 1;
while (portal->fetchRow())
{
cout << "Row : " << count << endl;
int intAttribute = atoi(portal->getData(0));
cout << "Integer attribute: " << intAttribute << endl;
TeTime t = portal->getDate(1);
cout << "Time attribute: " ;
cout << t.getDateTime("DDsMMsYYYYsHHsmmsSS") << " or " << t.getDateTime("YYYYsMMsDDsHHsmmsSS") << endl;
cout << "Double attribute: " ;
double d = portal->getDouble(2);
cout << d << endl;
cout << "String attribute: ";
string x = portal->getData(3);
cout << x << endl << endl;
count++;
}
delete portal;
db->close();
cout << endl << "Press Enter\n";
getchar();
return 0;
}
|