Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access Clipper data from C

Status
Not open for further replies.

maheshmadhyastha

Programmer
Apr 18, 2003
2
IN
I would like to access Clipper data from C program.
I need to use SQL statements to insert some values from C to Clipper data base.
Do I need to use any ODBC for the same ?
Pls let me know how I achive my requireement ?
I am very new to Clipper.

-Mahesh
 
If you are using the standard DBFNTX databasedrivers, you'll have to get an ODBC driver to go through ODBC connection for retrieving the data. If accessing directly, it depends wether you have functions/objects that can read .DBF files.
For 1 time conversion I'd write a Clipper or Foxpro program that exports all data to .csv format and import that into your preferred SQL environment.
For continues access: You'll have troubles when trying to lock records using either of the forementioned methods :-(

HTH
TonHu
 
Thx Tonhu.
I need to insert data every time into Clipper (which is in Xbase) and need to access data from Clipper table for further processing in SQL server (I need to insert values into SQL server).
if you have any idea on these, Pls let me know.

TIA,
Mahesh
 
check into CodeBase.. Its a library for C/C++ that allows full data and index support for dBase/Clipper/NDX/NTX/CDX.
I have experience with it, and it worked great.
Good Luck
 
Some c-code to access a dbf.
In the example any dbf passed as parameter will be listed.
Please adjust the code to your own needs.
This is MSC-6 code.
The nandef.h header file you should find it with your clipper compiler.


dbf.h header file
/***
* dbf.h
*/

typedef struct
{
char dbf_id;
char last_update[3];
long last_rec; /* unsigned ?? */
unsigned data_offset;
unsigned rec_size;
char filler[20];
} DBF_HEAD;


typedef struct
{
char field_name[11];
char field_type;
char dummy[4];
union
{
unsigned char_len;
struct
{
char len;
char dec;
} num_size;
} len_info;

char filler[14];
} FIELD_REC;

Rob.

dbflist.c source:


/***
* dbflist.c
*
* Stand alone code to list the contents of a database file.
* DBF file name is passed on command line, include .dbf extension.
*
* 1 - Ensure parameter is passed.
*
* 2 - Open file.
*
* 3 - Read header.
*
* 4 - Build linked list of FIELD_RECs.
*
* 5 - Seek to start of data.
*
* 6 - For each record:
*
* 6.1 - Read it.
*
* 6.2 - Print deleted character (* or space)
*
* 6.3 - For each field:
*
* 6.3.1 - Print it.
*/

/* headers for functions we use */
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#include "string.h"
#include "io.h"
#include "malloc.h"

#include "nandef.h"

#include "dbf.h"


typedef struct fl_tag
{
FIELD_REC field_rec;
struct fl_tag *next_flist;
} FIELD_LIST;


main(argc, argv)

int argc;
char *argv[];

{
FIELD_REC field_rec;
FIELD_LIST *field_list, *flist_ptr, *last_flist;
DBF_HEAD dbf_head;
int handle;
Boolean more_fields;
unsigned rec_num, rec_offset, width, num_fields;
char *record, *buffer;

if (argc != 2)
{
printf(&quot;Gebruik: dbflist <filenaam>\n&quot;);
exit(1);
}

handle = open(argv[1], O_RDONLY | O_RAW);
if (handle == -1)
{
printf(&quot;%s niet kunnen openen\n&quot;, argv[1]);
exit(1);
}

if (read(handle, (char *) &dbf_head, sizeof(dbf_head))
!= sizeof(DBF_HEAD))
{
printf(&quot;\nLeesfout\n&quot;);
exit(1);
}

/* build dynamic field_list linked list */
last_flist = NULL;

more_fields = TRUE;
num_fields = 0;

/* for each field */
while (more_fields)
{
more_fields = (read(handle, (char *) &field_rec,
sizeof(FIELD_REC)) == sizeof(FIELD_REC));

if (more_fields)
{
/* is this last field ? */
more_fields = (field_rec.field_name[0] != 0x0D);
if (more_fields)
{
/* no, add to list */
num_fields = num_fields + 1;
flist_ptr = (FIELD_LIST *) malloc(sizeof(FIELD_LIST));

memcpy((char *) &flist_ptr -> field_rec,
(char *) &field_rec, sizeof(FIELD_LIST));

flist_ptr -> next_flist = NULL;

/* is this the first entry in list ?? */
if (last_flist)
{
/* no, add to list */
last_flist -> next_flist = flist_ptr;
last_flist = flist_ptr;
}
else
{
/* yes, start list */
field_list = flist_ptr;
last_flist = field_list;
}
}
}
}

/* Now have field list built, list records */
flist_ptr = field_list;

/* position file to start of data */
if (lseek(handle, (long) dbf_head.data_offset, 0) == -1l)
{
printf(&quot;Fout bij zoeken in database file\n&quot;);
exit(1);
}

/* allocate space for 1 record */
record = (char *) malloc(dbf_head.rec_size);

/* This will cater for record with 1 field */
buffer = (char *) malloc(dbf_head.rec_size);

printf(&quot;\n\n&quot;);

/* for each record ... */
for (rec_num = 1; rec_num <= dbf_head.last_rec; rec_num++)
{
read(handle, record, dbf_head.rec_size);
printf(&quot;%4d &quot;, rec_num);

/* for each field */
flist_ptr = field_list;

/* print deleted char */
printf(&quot;%c &quot;, record[0]);

rec_offset = 1;
while (flist_ptr)
{
switch (flist_ptr -> field_rec.field_type)
{
case 'N':
width = flist_ptr ->
field_rec.len_info.num_size.len;
break;

default:
width = flist_ptr -> field_rec.len_info.char_len;
}

strncpy(buffer, (char *) &record[rec_offset], width);
buffer[width] = '\0';

printf(&quot;%s &quot;, buffer);
rec_offset = rec_offset + width;
flist_ptr = flist_ptr -> next_flist;
}
printf(&quot;\n&quot;);
}
}

/* List the contents of a database file */
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top