Created
November 20, 2014 03:48
-
-
Save forsythetony/a49325225bc568bef3b6 to your computer and use it in GitHub Desktop.
dont wanna hear it
This file contains hidden or 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
#include <stdio.h> | |
#include <sqlite3.h> | |
#include <string.h> | |
#define MAXSTRING 200 | |
// Function prototypes | |
void closeStatement( sqlite3_stmt *statement ); | |
void closeConnection( sqlite3 *connection ); | |
int checkIfFileExists( char* fileName ); | |
int main(int argc, char** argv) | |
{ | |
if(argc != 4) | |
{ | |
fprintf(stderr, "USAGE: %s <database file> <table name> <CSV file>\n", argv[0]); | |
return 1; | |
} | |
if( checkIfFileExists( argv[3] ) == 0 ) { | |
printf( "\nCSV file found.\n"); | |
} | |
else | |
{ | |
printf( "\nAn error occurred attempting to open the CSV file.\n\tExiting program now.\n"); | |
return -3; | |
} | |
// Open a connection to the database | |
sqlite3 *conn; | |
sqlite3_stmt *statement; | |
int dbhandle = sqlite3_open( argv[1], &conn ); | |
if( dbhandle != SQLITE_OK ) | |
{ | |
printf("Error opening database file. Exiting now."); | |
return -2; | |
} | |
char *select = "SELECT * FROM mytable;"; | |
int numCol = 0; | |
if( sqlite3_prepare_v2(conn, select, -1, &statement, 0) == SQLITE_OK) | |
{ | |
numCol = sqlite3_column_count( statement ); | |
printf("\n"); | |
FILE *file = fopen( argv[3] , "w" ); | |
while( sqlite3_step( statement) == SQLITE_ROW ) | |
{ | |
int i = 0; | |
for( i; i < numCol ; i++ ) | |
{ | |
int columnType = sqlite3_column_type( statement, i ); | |
switch( columnType ) | |
{ | |
case SQLITE_TEXT: | |
{ | |
char *resultString = (char*)sqlite3_column_text( statement, i ); | |
printf(" %s ", resultString ); | |
fprintf( file , "%s", resultString ); | |
break; | |
} | |
case SQLITE_INTEGER: | |
{ | |
int resultInteger = (int)sqlite3_column_int( statement, i); | |
printf( " %d ", resultInteger ); | |
fprintf( file, "%d", resultInteger ); | |
break; | |
} | |
default: | |
printf( "WRONG!"); | |
} | |
if( i < numCol - 1 ) | |
{ | |
fprintf( file, ","); | |
} | |
else | |
{ | |
fprintf( file, "\n"); | |
} | |
} | |
printf( "\n"); | |
} | |
printf("\n"); | |
fclose(file); | |
} | |
// Finalize the statement | |
closeStatement( statement ); | |
// Close connection to the database | |
closeConnection( conn ); | |
printf( "\n" ); | |
return 0; | |
} | |
void closeStatement( sqlite3_stmt *statement ) { | |
int closeResult = sqlite3_finalize( statement ); | |
switch( closeResult ) | |
{ | |
case SQLITE_OK: | |
{ | |
printf( "\nThe statement was successfully finalized." ); | |
break; | |
} | |
default: | |
printf( "\n"); | |
} | |
} | |
void closeConnection( sqlite3 *connection ) { | |
int closeResult = sqlite3_close( connection ); | |
switch( closeResult ) | |
{ | |
case SQLITE_OK: | |
{ | |
printf( "\nThe database was successfully closed."); | |
break; | |
} | |
case SQLITE_BUSY: | |
{ | |
printf( "\nThe database was busy. This could be due to statements that could not be finalized." ); | |
break; | |
} | |
default: | |
printf( "\nAn unknown error occurred and the database could not be closed."); | |
} | |
} | |
int checkIfFileExists( char* fileName ) { | |
FILE *file; | |
if (( file = fopen( fileName, "w" )) == NULL ) { | |
return -1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment