-
-
Save forsythetony/98967ba890f67a193fac to your computer and use it in GitHub Desktop.
fixurshit
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 main(int argc, char** argv) | |
{ | |
if(argc != 4) | |
{ | |
fprintf(stderr, "USAGE: %s <database file> <table name> <CSV file>\n", argv[0]); | |
return 1; | |
} | |
// 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"); | |
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 ); | |
break; | |
} | |
case SQLITE_INTEGER: | |
{ | |
int resultInteger = (int)sqlite3_column_int( statement, i); | |
printf( " %d ", resultInteger ); | |
break; | |
} | |
default: | |
printf( "WRONG!"); | |
} | |
} | |
printf( "\n"); | |
} | |
printf("\n"); | |
} | |
// 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."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment