1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "ocilib.h"
void export_table(OCI_Connection *cn, char *table)
{
OCI_Statement *st = NULL;
OCI_Resultset *rs = NULL;
int i, nb = 0;
FILE *f = NULL;
char buf[100] = "";
sprintf(buf, "%s.csv", table);
f = fopen(buf, "w");
if (f)
{
st = OCI_StatementCreate(cn);
if (st)
{
sprintf(buf, "select * from %s", table);
OCI_ExecuteStmt(st, buf);
rs = OCI_GetResultset(st);
nb = OCI_GetColumnCount(rs);
while (OCI_FetchNext(rs))
{
for (i = 1; i <= nb; i++)
{
char *s = OCI_GetString(rs, i);
fprintf(f, s != NULL ? s : "");
fprintf(f, i == nb ? "\n" : ";");
}
}
OCI_StatementFree(st);
}
fclose(f);
}
}
int main(void)
{
OCI_Connection *cn = NULL;
if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
return EXIT_FAILURE;
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
if (cn)
{
export_table(cn, "table1");
export_table(cn, "table2");
}
OCI_ConnectionFree(cn);
OCI_Cleanup();
return EXIT_SUCCESS;
} |
Partager