This shows you the differences between two versions of the page.
| — |
oaenv:examples:c_oa_wine [2012/10/26 14:26] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{page>:top_add&nofooter&noeditbtn}} | ||
| + | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
| + | ====== C Example using OmniAccess API ====== | ||
| + | <code c> | ||
| + | #include <stdio.h> | ||
| + | #include <string.h> | ||
| + | #include "oa.h" | ||
| + | |||
| + | typedef struct | ||
| + | { | ||
| + | char name[30]; | ||
| + | char mfg[25]; | ||
| + | char price[6]; | ||
| + | char year[2]; | ||
| + | char state[2]; | ||
| + | } winestruct; | ||
| + | |||
| + | char message[128]; | ||
| + | char critprompt[30]; | ||
| + | char temp[1]; | ||
| + | |||
| + | int instance, cursor; | ||
| + | int OASTAT_END_OF_DATA = 11032; | ||
| + | oastatus_t status; | ||
| + | winestruct winebuf; | ||
| + | |||
| + | main() | ||
| + | { | ||
| + | |||
| + | /* Connect to environment catalog called WINES.EC */ | ||
| + | |||
| + | if (oaconnect("WINES.EC", "", &status, "", "", &instance)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("Connected to dictionary.\n\n"); | ||
| + | |||
| + | /* Open a cursor. */ | ||
| + | |||
| + | if (oaopencursor(instance, "", &status, &cursor)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("opened cursor \n\n"); | ||
| + | |||
| + | /* Prompt the user for a WINE-NAME to retrieve on. */ | ||
| + | |||
| + | printf("Type / to stop.\n\n"); | ||
| + | printf("Enter a wine name or names: "); | ||
| + | gets(critprompt); | ||
| + | |||
| + | /* Loop to do numerous retrievals. */ | ||
| + | |||
| + | while (critprompt[0] != '/') | ||
| + | { | ||
| + | temp[0] = 's'; | ||
| + | |||
| + | /* Qualify on the WINE-NAME field in the BOTTLES table */ | ||
| + | |||
| + | if (oaqualify(cursor, "", &status, "", "BOTTLES", | ||
| + | "WINE-NAME", &critprompt)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("Qualified entries = %d \n\n", status.count); | ||
| + | |||
| + | /* status.count = # of qualified records. If it's not 0 (i.e. we've | ||
| + | found some records to retrieve), we'll select and fetch them. */ | ||
| + | |||
| + | if (status.count != 0) | ||
| + | { | ||
| + | |||
| + | /* Select the qualified records from BOTTLES, and get the following | ||
| + | fields: WINE-NAME, WINE-MFG, WINE-PRICE, WINE-YEAR, and WINE-STATE. */ | ||
| + | |||
| + | if (oaselect(cursor, "", &status, "BOTTLES", | ||
| + | "WINE-NAME, WINE-MFG, WINE-PRICE, WINE-YEAR, WINE-STATE", | ||
| + | "$RECNO=$ODXID")) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("Performed select.\n\n"); | ||
| + | |||
| + | /* While we haven't reached the end-of-file, loop to fetch the records. */ | ||
| + | |||
| + | while ((status.error != OASTAT_END_OF_DATA) && (temp[0] != 'n')) | ||
| + | { | ||
| + | |||
| + | /* Fetch the records, 1 at a time, into the buffer winebuf and then | ||
| + | display them if there's no error. */ | ||
| + | |||
| + | if (oafetch(cursor, "", &status, 1, &winebuf)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | { | ||
| + | printf("WINE-NAME: %-30.30s\n", winebuf.name); | ||
| + | printf("WINE-MFG: %-25.25s\n", winebuf.mfg); | ||
| + | printf("WINE-PRICE: %-6.6s\n", winebuf.price); | ||
| + | printf("WINE-YEAR: %-2.2s\n", winebuf.year); | ||
| + | printf("WINE-STATE: %-2.2s\n\n\n", winebuf.state); | ||
| + | printf("View next record? (y/n) "); | ||
| + | gets(temp); | ||
| + | printf("\n\n\n"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | printf("Type / to stop.\n\n"); | ||
| + | printf("Enter a wine name or names: "); | ||
| + | gets(critprompt); | ||
| + | } | ||
| + | |||
| + | /* Close the cursor. */ | ||
| + | |||
| + | if (oaclosecursor(cursor, "", &status)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("Closed cursor \n\n"); | ||
| + | |||
| + | /* Disconnect from the catalog. */ | ||
| + | |||
| + | if (oadisconnect(instance, "", &status)) | ||
| + | printf("%s\n", oaerror(&status, "", message)); | ||
| + | else | ||
| + | printf("Disconnected \n\n"); | ||
| + | } | ||
| + | </code> | ||
| + | {{page>:bottom_add&nofooter&noeditbtn}} | ||