Created
June 14, 2014 18:31
-
-
Save ei-grad/b9bdcb4ec3b77575b235 to your computer and use it in GitHub Desktop.
Convert Paradox DB table to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sudo apt-get install pxlib | |
// gcc -std=c99 -lpx px2csv.c -o px2csv | |
#include <paradox.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) { | |
FILE *out; | |
int ret; | |
int numrecords, numfields; | |
pxdoc_t *pxdoc; | |
char * buf; | |
PX_boot(); | |
pxdoc = PX_new(); | |
if (argc == 3) { | |
PX_open_file(pxdoc, argv[1]); | |
out = fopen(argv[2], "w"); | |
} else { | |
fprintf(stderr, "usage: px2csv <infile.db> <outfile.csv>\n"); | |
exit(1); | |
} | |
numrecords = PX_get_num_records(pxdoc); | |
numfields = PX_get_num_fields(pxdoc); | |
fprintf(stderr, "num records: %d\n", numrecords); | |
for (int i=0; i<numrecords; i++) { | |
struct px_val ** record; | |
record = PX_retrieve_record(pxdoc, i); | |
for (int j=0; j < numfields; j++) { | |
if (record == NULL) { | |
fprintf(stderr, "PX_retrieve_record failed for record %d in field %d\n", i, j); | |
} else if ((*record)->isnull == 1) { | |
fprintf(out, "NULL"); | |
} else { | |
switch ((*record)->type) { | |
case pxfAlpha: | |
fprintf(out, "%s", (*record)->value.str.val); | |
break; | |
case pxfShort: | |
case pxfLong: | |
case pxfDate: | |
case pxfTime: | |
case pxfLogical: | |
case pxfAutoInc: | |
fprintf(out, "%ld", (*record)->value.lval); | |
break; | |
case pxfTimestamp: | |
case pxfNumber: | |
case pxfCurrency: | |
fprintf(out, "%lf", (*record)->value.dval); | |
break; | |
default: | |
fprintf(out, "<unknown_type:%d>", (*record)->type); | |
break; | |
} | |
} | |
record++; | |
if (j < numfields - 1) fprintf(out, ":"); | |
} | |
fprintf(out, "\n"); | |
} | |
fclose(out); | |
PX_close(pxdoc); | |
PX_delete(pxdoc); | |
PX_shutdown(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment