Chapter 2. Get in contact with a table

Below you see the program of chapter 1 with some additional commands. A hk_database object represents a database, the name of the database can be set either with the constructor or with "set_name(const string&)".

A table or a query is be represented by a hk_datasource object (a query with a "SELECT statement is called in hk_classes a resultquery and can be created with hk_datasource* mydatasource=mydatabase->new_resultquery(); ).

Before you can see the data of a datasource you have to enable it (then the SQL-statement will be executed). The last command ("dump_data()") is just added so that you can see the data, please don't use it in your code.


Example 2-1. Get in contact with a table

   1 
   2   #include <hk_classes.h>
   3   #include <iostream>
   4   int main()
   5   {
   6   hk_drivermanager* mydrivermanager = new hk_drivermanager();
   7   if (mydrivermanager==NULL) {cout <<"error creating mydrivermanager"<<endl;exit(1);}
   8   hk_connection* myconnection = mydrivermanager->new_connection();
   9   if (myconnection==NULL) {cout <<"error creating myconnection"<<endl;exit(1);}
  10   myconnection->connect();
  11 
  12   hk_database* mydatabase=myconnection->new_database("exampledb");
  13   if (mydatabase==NULL) {cout <<"error creating mydatabase"<<endl;exit(1);}
  14   hk_datasource* mydatasource= mydatabase->new_table("authors");
  15   if (mydatasource==NULL) {cout <<"error creating mydatasource"<<endl;exit(1);}
  16   mydatasource->enable();
  17   //the following internal debugging command should not be used. It is used here for
  18   //demonstration purposes only!!!! 
  19   mydatasource->dump_data(); // DON'T USE THIS COMMAND IN YOUR PROGRAMMS!!!
  20 
  21   delete mydrivermanager;
  22   }