added update
+140 −106 · 1 file changed
-
graph/db/sqltest.c+140 −106
@@ -17,16 +17,17 @@ char *sSql = "DROP TABLE IF EXISTS Friends;" "INSERT INTO Friends(Name,Age) VALUES ('Roger',12);" "INSERT INTO Friends(Name,Age) VALUES ('Robert',16);"; - -// -------------------------------------------------------------------------------------------------- +char *uSql = "UPDATE Friends SET Name = 'Jerry' WHRE id = 1;"; +// ---------------------------------FUNCTION DECLARATIONS-------------------------------------------- int mainmenu(void); int dbInsert(sqlite3 *db, char *data); int dbSelect(sqlite3 *db, char *data); -//int callback(sqlite3_stmt *res ); -// -------------------------------------------------------------------------------------------------- +int dbUpdate(sqlite3 *db, char *data); +static int callback(void *data, int argc, char **argv, char **azColName ); +// ---------------------------------START MAIN------------------------------------------------------ int main(int argc, char **argv) { - + // 1. Definitions // Each opened SQLite database is represented by a pointer // to an instance of the opaque structure named sqlite3 @@ -45,121 +46,129 @@ int main(int argc, char **argv) { // Return error code return 1; } else if (argc == 3) { - - // 2. Get the return code from sqlite3_open + + // 2. Get the return code from sqlite3_open #if MEMWRITE == 1 - rc = sqlite3_open(":memory:", &db); + rc = sqlite3_open(":memory:", &db); #else - rc = sqlite3_open("sqltest.db", &db); + rc = sqlite3_open("sqltest.db", &db); #endif - // Test that the return code is OK - if (rc != SQLITE_OK) { + // Test that the return code is OK + if (rc != SQLITE_OK) { - fprintf(stderr,"Error opening sqlite3 database in memory %s\n",sqlite3_errmsg(db) ); - fprintf(stderr,"Clearing resoursces used for sqlite3_open\n"); + fprintf(stderr,"Error opening sqlite3 database in memory %s\n",sqlite3_errmsg(db) ); + fprintf(stderr,"Clearing resoursces used for sqlite3_open\n"); - // Whether or not an error occurs when it is opened, - // resources associated with the database connection - // handle should be released by passing it to - // sqlite3_close() when it is no longer required. - sqlite3_close(db); - fprintf(stderr,"Database closed.\n"); + // Whether or not an error occurs when it is opened, + // resources associated with the database connection + // handle should be released by passing it to + // sqlite3_close() when it is no longer required. + sqlite3_close(db); + fprintf(stderr,"Database closed.\n"); - // Return error code - return 1; + // Return error code + return 1; - } + } - // Let us know if the database connection was succesfull. - fprintf(stdout,"Opened database connection succesfully\n"); + // Let us know if the database connection was succesfull. + fprintf(stdout,"Opened database connection succesfully\n"); - // Display the SQLite version - debug + // Display the SQLite version - debug #if DEBUG == 1 - // The sqlite3_stmt represents a single SQL statement - sqlite3_stmt *res; - // Seeing if the global variable is printed - fprintf(stdout,"The SQL statement is: %s\n", sSql); - // The sqlite3_prepare is the constructor for the sqlite3_stmt - // compile sql text into byte code that will do the work for querying - // 1st arg - database connection - obtained from sqlite3_open() - // 2nd arg - zSql - statement to be compiled - // 3rd arg - nByte - negative -> read up to the first zero terminator - // 4th arg - *ppStmt pointing to a compiled statement that can be run with sqlite3_step() - // the calling proc is responsable for deleting the SQL statement with sqlite3_finalize() - // 5th arg - pzTail point to the first byte past the end of the first SQL staement - rc = sqlite3_prepare_v2(db, "SELECT sqlite_version()", -1, &res, 0); - fprintf(stderr,"%d\n\r",rc); - if (rc == SQLITE_ROW) { - fprintf(stdout,"SQLite Version is: %s\n",sqlite3_column_text(res,0)); - } + // The sqlite3_stmt represents a single SQL statement + sqlite3_stmt *res; + // Seeing if the global variable is printed + fprintf(stdout,"The SQL statement is: %s\n", sSql); + // The sqlite3_prepare is the constructor for the sqlite3_stmt + // compile sql text into byte code that will do the work for querying + // 1st arg - database connection - obtained from sqlite3_open() + // 2nd arg - zSql - statement to be compiled + // 3rd arg - nByte - negative -> read up to the first zero terminator + // 4th arg - *ppStmt pointing to a compiled statement that can be run with sqlite3_step() + // the calling proc is responsable for deleting the SQL statement with sqlite3_finalize() + // 5th arg - pzTail point to the first byte past the end of the first SQL staement + rc = sqlite3_prepare_v2(db, "SELECT sqlite_version()", -1, &res, 0); + fprintf(stderr,"%d\n\r",rc); + if (rc == SQLITE_ROW) { + fprintf(stdout,"SQLite Version is: %s\n",sqlite3_column_text(res,0)); + } #endif - // 3.Inserting data - calls dbInsert - // 1st arg - open database handle - // 2nd arg - string to insert - if (!dbInsert(db,sSql) ) { - fprintf(stdout,"Inserted data succesfully\n"); - } - char *zSelect = "SELECT * from Friends;"; - if (!dbSelect(db,zSelect) ) { - fprintf(stdout,"Select data succesfully\n"); - } - - // Close the database connection. - sqlite3_close(db); - } else { - mainmenu(); - } - return 0; + // 3.Inserting data - calls dbInsert + // 1st arg - open database handle + // 2nd arg - string to insert + if (!dbInsert(db,sSql) ) { + fprintf(stdout,"Inserted data succesfully\n"); + } + char *zSelect = "SELECT * from Friends;"; + if (!dbSelect(db,zSelect) ) { + fprintf(stdout,"Select data succesfully\n"); + } + // update + if (!dbUpdate(db,uSql) ) { + fprintf(stdout,"Updated data succesfully\n"); + } + //if (!dbSelect(db,zSelect) ) { + // fprintf(stdout,"Select data succesfully\n"); + // } + + // Close the database connection. + sqlite3_close(db); + } else { + // Execute the main menu function + mainmenu(); + } + return 0; } -// -------------------------------------------------------------------------------------------------- +// -----------------------------END MAIN------------------------------------------------------------- int mainmenu(void) { - char menu[] = - "\n-----------------------\n" - "1. View database entries\n" - "2. Update database records\n" - "3. Delete database record\n" - "4. Quit\n" - "\n-----------------------\n"; - - //displaymenu - // promptuser - // switch responses for each entry - //last item should be quit as long - //as there are no errors - char response[128]; - unsigned int item; - for (;;) { - fputs(menu,stdout); - - fputs("Enter a choice > ",stdout); - // fflush(); - - if(fgets(response, sizeof(response), stdin) == NULL) { - exit(1); - } - if(sscanf(response,"%u",&item) != 1) { - fputs("Error reading commnad. Please try again\n",stdout); - } else { - - switch (item) { - case 1: - fputs("dbSelect()\n",stdout); - break; - case 2: - fputs("Option 2\n",stdout); - break; - default: - fputs("Please choose a valid menu option\n",stdout); - } - } - return 0; - } + char menu[] = + "\n-----------------------\n" + "1. View database entries\n" + "2. Update database records\n" + "3. Delete database record\n" + "4. Quit\n" + "\n-----------------------\n"; + + //displaymenu + //promptuser/ + //switch responses for each entry / + //last item should be quit as long + //as there are no errors + char response[128]; + unsigned int item; + for (;;) { + fputs(menu,stdout); + + fputs("Enter a choice > ",stdout); + // fflush(); + + if(fgets(response, sizeof(response), stdin) == NULL) { + exit(1); + } + if(sscanf(response,"%u",&item) != 1) { + fputs("Error reading commnad. Please enter a valid integer\n",stdout); + } else { + + switch (item) { + case 1: + fputs("dbSelect()\n",stdout); + break; + case 2: + fputs("Option 2\n",stdout); + break; + default: + fputs("Please choose a valid menu option\n",stdout); + } + } + return 0; + } } int dbInsert(sqlite3 *db, char *data) { @@ -193,7 +202,7 @@ int dbInsert(sqlite3 *db, char *data) { // Select data // 1st arg an open db -// 2nd arg table +// 3nd arg table // 3rd arg condition/selection // rows @@ -251,9 +260,6 @@ int dbSelect(sqlite3 *db, char *data) { // Grab next row rc = sqlite3_step(res); } - - - // Finalize - clean up after using. rc = sqlite3_finalize(res); if ( rc != SQLITE_OK ) { @@ -268,8 +274,36 @@ int dbSelect(sqlite3 *db, char *data) { return 0; } -// int (*callback)(sqlite_stmt *res, +static int callback(void *data, int argc, char **argv, char **azColName ) { + + + return 0; +} + // Update data +int dbUpdate (sqlite3 *db, char *data) { + sqlite3_stmt *res; + // Vars + const char *msg = "Called callback function\n"; + char *zErrMsg = NULL; + int rc = 0; + // Check that db is presnet + if ( db == NULL ) { + fprintf(stderr,"Error with database access, exiting\n"); + return 1; + } + // Prepare & check + rc = sqlite3_prepare_v2(db, data, -1, &res, 0); + if (rc != SQLITE_OK) { + fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc); + sqlite3_free(zErrMsg); + sqlite3_close(db); + + return 1; + } + rc = sqlite3_exec (db, data,callback,(void*) msg, &zErrMsg); + fprintf(stdout,"%d\n",rc); +} // Delete data