~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to examples/importDBF/importDBF.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
Import upstream version 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
/* 
 
25
        This file shows an example of how to import a DBF table as
 
26
        an attribute table in a TerraLib database. This attribute 
 
27
        table will be registered in the database as an external table.
 
28
 
 
29
        Author: Lubia Vinhas  
 
30
*/
 
31
 
 
32
#include <TeDatabase.h>
 
33
#include <TeImportExport.h>
 
34
#include <TeMySQL.h>
 
35
 
 
36
int main()
 
37
{
 
38
     // Database server parameters
 
39
     string host = "localhost";
 
40
     string dbname = "TerraTeste";
 
41
     string user = "root";
 
42
     string password = "";
 
43
 
 
44
     // Connect to the database
 
45
     TeDatabase* db = new TeMySQL();
 
46
     if (!db->connect(host, user, password, dbname))
 
47
     {
 
48
                cout << "Error: " << db->errorMessage() << endl << endl;
 
49
                cout << "Press Enter\n";
 
50
                return 1;
 
51
     }
 
52
 
 
53
        // Import the dbf file
 
54
        string filename = "../data/SOCEC.dbf";
 
55
        if (db->tableExist("SOCEC"))
 
56
        {
 
57
                cout << "There is already a table named \"SOCEC\" in the TerraLib database!\n\n";
 
58
                cout << "Press Enter\n";
 
59
                getchar();
 
60
                return 1;
 
61
        }
 
62
 
 
63
        if (TeImportDBFTable(filename,db)) 
 
64
                cout << "The dbf file \"SOCEC.dbf\" was imported successfully as an external table in the TerraLib database!\n\n";
 
65
        else
 
66
        {
 
67
                db->close();
 
68
                cout << "Fail to import the dbf file!\n";
 
69
                cout << "Press Enter\n";
 
70
                getchar();
 
71
                return 1;
 
72
        }
 
73
 
 
74
        TeAttrTableVector tableVec;
 
75
        if (db->getAttrTables(tableVec, TeAttrExternal))
 
76
        {
 
77
                cout << "External tables in the database: " << tableVec.size() << endl;
 
78
                TeAttrTableVector::iterator it = tableVec.begin();
 
79
                while (it != tableVec.end())
 
80
                {
 
81
                        cout << (*it).name() << " primary key column: " << (*it).uniqueName() << endl << endl;
 
82
                        ++it;
 
83
                }
 
84
        }
 
85
 
 
86
        db->close();
 
87
        cout << "Press Enter\n";
 
88
        getchar();
 
89
        return 0;
 
90
}
 
91