Prepare HBasic for database access


Important information:

Overview

To access databases from HBasic you normally do not need to install something special. HBasic uses the QT libraries in a version >= 3.0 and this libraries already have build in support to access tables or data within a database.  The current release of QT 3.0 supports plugins for the following types of database server.

      QODBC3 - ODBC (Open Database Connectivity) Driver
      QOCI8 - Oracle Call Interface Driver
      QPSQL7 - PostgreSQL v6.x and v7.x Driver
      QTDS7 - Sybase Adaptive Server and Microsoft SQL Server Driver
      QMYSQL3 - MySQL Driver

This means with QT >= 3.0 there is no need to install additional libraries (for ODBC or other backends). This document will describe two steps that may be helpful when trying to start with databases in HBasic. For the example programs I started with the MySQL plugin and the MySQL server software. Therefore in the following description I assume that you also have installed the MySQL Server and client software. If anybody will have problems to use HBasic or the example programs with other server software any information will be welcome.



Compile a QT plugin for MySQL


When you have downloaded the QT sourcecode and created the QT library through compiling you do not automatically have compiled the database plugin. This must be done in a separate make command. I think the reason why TrollTech did it this way was that the database plugin needs additional information about the database client software it should work with. This is especially the path where the database client has been installed and the version of the client.

To compile the MySQL plugin for QT do the following.

Go to the  plugins/src/sqldrivers/mysql subdirectory of your QT directoty ($QTDIR).
Have a look at the README file in this directory for further information about how to compile the plugin.
Create the Makefile to compile the MySQL plugin with a qmake command similar to the one in the README file. My be you have to change the path to your mysql include-files and mysql shared libraries. Depending on the installation of your Linux distribution this should be /usr/local/include or /usr/include/mysql for the INCLUDEPATH and /usr/local/libs or /usr/lib/mysql for the LIBS.

For my installation I put the following command into a new skript, made it executable (chmod 777 <scriptname>) and started the script.

qmake -o Makefile "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient" mysql.pro

After this qmake should have generated a new Makefile so that you now may start make to compile the plugin. If you do not compile the QT plugin that you want to reference in your HBasic code you will get an error message when starting the program.



Create and configure a test database for HBasic examples


If you have started your MySQL server (see the MySQL documentation how to install and start the server) you may now prepare a database to execute the examples that will be used in the HBasic documentation and delivered with HBasic. If you do not want to start the examples there is no need to read and execute the following with one exception.  

Create database with MySQL before HBasic may access it

As with ODBC the QT database driver may only connect to an existing database. You cannot connect to the database system himself and execute a SQL statement like "create database...". Therefore you have to create every new database with mysql and may then switch to HBasic to create or access the tables within this database. Mayby anyone knows a solution to this problem.

For the example programs we want to create a database with name "hbasic " and two tables with name "tab1" and "tab2". To create this database execute the following statements with the mysql client.

info You can find a SQL script (create_db.sql) to create the database in the info/sql directory of your hbasic installation path. When you start this script with a database client like mysql it will create the database with some small tables that can be used fortesting purposes.

Log in to the mysql client

mysql -u username -p
enter password for your database user

create database hbasic;

create table tab1( col1 integer primary key, col2 integer, col3 integer);

create table tab2( field1 integer, dbl2 double, str3 char(30));

Insert some data into the new created tables with the statements

insert into tab1 values( 1,11,111 );
insert into tab1 values( 2,22,222 );
insert into tab1 values( 333,33,3 );
 etc.

Check contents of table tab1 with the statement

select * from tab1;

when you are ready leave the mysql client with the command quit.

This will be all to start the first examples with using HBasic database access.