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


  #define HAVE_SSTREAM 1
  #include <hk_classes.h>
  #include <iostream>
  int main()
  {
  hk_drivermanager* mydrivermanager = new hk_drivermanager();
  if (mydrivermanager==NULL) {cout <<"error creating mydrivermanager"<<endl;exit(1);}
  hk_connection* myconnection = mydrivermanager->new_connection();
  if (myconnection==NULL) {cout <<"error creating myconnection"<<endl;exit(1);}
  myconnection->connect();

  hk_database* mydatabase=myconnection->new_database("exampledb");
  if (mydatabase==NULL) {cout <<"error creating mydatabase"<<endl;exit(1);}
  hk_datasource* mydatasource= mydatabase->new_table("authors");
  if (mydatasource==NULL) {cout <<"error creating mydatasource"<<endl;exit(1);}
  mydatasource->enable();
  //the following internal debugging command should not be used. It is used here for
  //demonstration purposes only!!!! 
  mydatasource->dump_data(); // DON'T USE THIS COMMAND IN YOUR PROGRAMMS!!!

  delete mydrivermanager;
  }