Subject | How to Connect to firebird database using the API, registring server and database with Firebird Workbench. Some tips. |
---|---|
Author | Henrik Sitter |
Post date | 2003-01-10T15:04:57Z |
Hello,
I have been asking some questions here, and received good help. I
thought I should give some back and tell something about how to connect
to a firebird database using the API. I don't know if it's of any
interest but here we go:
First of all, this is by far an all inclusive discussion, and the code I
provide is quite messy.
Second, I'm using Win XP Professional. I have installed Firebird v.1.0.0
(because I had some problems with version v.1.0.2). Firebird is running
as a service (see Control Panel->Administrative Tools->Services) as is
Firebird Guardian Service. I have used Firebird Workbench v.1.4.1 (trial
version, it looks good :), might buy it) to register the server and the
test database like this:
*For the Server:
Server: 111.222.333.444 // My IP address
Alias: Henrik
Protocol: TCP/IP
Username: Henrik
Pasword: // Not registered anything here
Architecture: SuperServer
SYSDBA Password: masterkey // This is still just a test application
*For the database:
Database: c:\Program Files\Firebird\Database\Henrik_Database\Henrik.fdb
Username: Henrik
Password: masterkey
Server: 111.222.333.444
Protocol: TCP/IP
If I press "User Manager" in Firebird Workbench, only the user SYSDBA is
listed so it seems to be the only registered user. So when I connect to
the database I use as user/password SYSDBA/masterkey even though Henrik
is the username I specified (see above).
Third, I'm using MSVC (Microsoft Visual C++ 6.0), and before compiling
and linking I had to specify some Project Settings. Press Alt+F7 to
reach Project Settings and on the Link Tab, add the file gds32_ms.lib
(not gds32.lib!!) where it says "Object/library modules". I tried with
gds32.lib first, but received an error. I think it's because VC++ is a
C++ compiler (and not C) that I had to choose gds32_ms.lib instead of
gds32.lib.
Next, press Tools->Options and choose the Directories Tab. Add the line
C:\Program Files\Firebird\include (that is, the place where you have
located your ibase.h file).
Ok. So far so good. It's time for the code (which is a bit messy and
very simple, but it connects to the database...):
#include <ibase.h> // The firebird/interbase header file that needs
to be #include <iostream> // included.
int main()
{
int testtall = 1; // Just a test variable .
isc_db_handle db1; // Database handle.
// Allocate some pointers to a dpb (database parameter buffer).
// You use the dpb to talk with the database.
char dpb_buffer[256], *dpb, *p;
short dpb_length;
char *uname; // user-name.
char *upass; // password.
ISC_STATUS status_vector[20]; // Status vector, to monitor
connection.
// I struggled a bit to get the correct TCP/IP syntax, but this
is it:
// servername:drive:\Path\Database.fdb, or
// tcp/ip-adress:drive:\Path\Database.fdb.
char *str = "111.222.333.444:c:\\Program
Files\\Firebird\\Database\\HENRIK_Database\\HENRIK.fdb";
uname = "SYSDBA";
upass = "masterkey";
db1 = NULL; // You MUST initialize the database handle to NULL.
dpb = dpb_buffer;
// Specify the version of the parameter buffer, always the
// compile-time constant isc_dpb_version1.
*dpb++ = isc_dpb_version1;
// # of cache buffers to allocate for use with the database,
default = // 75. In the API guide I think isc_dpb_num_buffers is
specified as // isc_num_buffers, but that I could not get to work.
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = 90;
*dpb++ = isc_dpb_user_name; // Save user-name in dpb.
*dpb++ = strlen(uname);
for (p = uname; *p;)
*dpb++ = *p++;
*dpb++ = isc_dpb_password; // Save password in dpb.
*dpb++ = strlen(upass);
for (p = upass; *p;)
*dpb++ = *p++;
dpb_length = dpb - dpb_buffer;
// The following methods could according to the API guide also be
used
// to register username and password, but I could not get it to work,
so // I used the metod above.
// isc_expand_dpb(&dpb, &dpb_length, isc_dpb_user_name, uname,
NULL);
// isc_expand_dpb(&dpb, &dpb_length, isc_dpb_password, upass,
NULL);
// Attach to the database.
isc_attach_database(status_vector, strlen(str), str, &db1,
dpb_length, dpb_buffer);
// Check if we connected. If testtall = 1 still after the
if-check, we // connected. Also check the database handle db1 to see
if we
// connected. If db1 != 0, then we connected.
if (status_vector[0] == 1 && status_vector[1])
{
testtall = 2;
return 0;
}
Well, that's it. Hope somebody find it a bit useful. If there are any
major errors please let me know.
It's probably better to use IB Objects, like Svein erling Tysvaer
suggested, but I think it's okay to know a bit about the API as well.
Henrik
I have been asking some questions here, and received good help. I
thought I should give some back and tell something about how to connect
to a firebird database using the API. I don't know if it's of any
interest but here we go:
First of all, this is by far an all inclusive discussion, and the code I
provide is quite messy.
Second, I'm using Win XP Professional. I have installed Firebird v.1.0.0
(because I had some problems with version v.1.0.2). Firebird is running
as a service (see Control Panel->Administrative Tools->Services) as is
Firebird Guardian Service. I have used Firebird Workbench v.1.4.1 (trial
version, it looks good :), might buy it) to register the server and the
test database like this:
*For the Server:
Server: 111.222.333.444 // My IP address
Alias: Henrik
Protocol: TCP/IP
Username: Henrik
Pasword: // Not registered anything here
Architecture: SuperServer
SYSDBA Password: masterkey // This is still just a test application
*For the database:
Database: c:\Program Files\Firebird\Database\Henrik_Database\Henrik.fdb
Username: Henrik
Password: masterkey
Server: 111.222.333.444
Protocol: TCP/IP
If I press "User Manager" in Firebird Workbench, only the user SYSDBA is
listed so it seems to be the only registered user. So when I connect to
the database I use as user/password SYSDBA/masterkey even though Henrik
is the username I specified (see above).
Third, I'm using MSVC (Microsoft Visual C++ 6.0), and before compiling
and linking I had to specify some Project Settings. Press Alt+F7 to
reach Project Settings and on the Link Tab, add the file gds32_ms.lib
(not gds32.lib!!) where it says "Object/library modules". I tried with
gds32.lib first, but received an error. I think it's because VC++ is a
C++ compiler (and not C) that I had to choose gds32_ms.lib instead of
gds32.lib.
Next, press Tools->Options and choose the Directories Tab. Add the line
C:\Program Files\Firebird\include (that is, the place where you have
located your ibase.h file).
Ok. So far so good. It's time for the code (which is a bit messy and
very simple, but it connects to the database...):
#include <ibase.h> // The firebird/interbase header file that needs
to be #include <iostream> // included.
int main()
{
int testtall = 1; // Just a test variable .
isc_db_handle db1; // Database handle.
// Allocate some pointers to a dpb (database parameter buffer).
// You use the dpb to talk with the database.
char dpb_buffer[256], *dpb, *p;
short dpb_length;
char *uname; // user-name.
char *upass; // password.
ISC_STATUS status_vector[20]; // Status vector, to monitor
connection.
// I struggled a bit to get the correct TCP/IP syntax, but this
is it:
// servername:drive:\Path\Database.fdb, or
// tcp/ip-adress:drive:\Path\Database.fdb.
char *str = "111.222.333.444:c:\\Program
Files\\Firebird\\Database\\HENRIK_Database\\HENRIK.fdb";
uname = "SYSDBA";
upass = "masterkey";
db1 = NULL; // You MUST initialize the database handle to NULL.
dpb = dpb_buffer;
// Specify the version of the parameter buffer, always the
// compile-time constant isc_dpb_version1.
*dpb++ = isc_dpb_version1;
// # of cache buffers to allocate for use with the database,
default = // 75. In the API guide I think isc_dpb_num_buffers is
specified as // isc_num_buffers, but that I could not get to work.
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = 90;
*dpb++ = isc_dpb_user_name; // Save user-name in dpb.
*dpb++ = strlen(uname);
for (p = uname; *p;)
*dpb++ = *p++;
*dpb++ = isc_dpb_password; // Save password in dpb.
*dpb++ = strlen(upass);
for (p = upass; *p;)
*dpb++ = *p++;
dpb_length = dpb - dpb_buffer;
// The following methods could according to the API guide also be
used
// to register username and password, but I could not get it to work,
so // I used the metod above.
// isc_expand_dpb(&dpb, &dpb_length, isc_dpb_user_name, uname,
NULL);
// isc_expand_dpb(&dpb, &dpb_length, isc_dpb_password, upass,
NULL);
// Attach to the database.
isc_attach_database(status_vector, strlen(str), str, &db1,
dpb_length, dpb_buffer);
// Check if we connected. If testtall = 1 still after the
if-check, we // connected. Also check the database handle db1 to see
if we
// connected. If db1 != 0, then we connected.
if (status_vector[0] == 1 && status_vector[1])
{
testtall = 2;
return 0;
}
Well, that's it. Hope somebody find it a bit useful. If there are any
major errors please let me know.
It's probably better to use IB Objects, like Svein erling Tysvaer
suggested, but I think it's okay to know a bit about the API as well.
Henrik