← cproj

bla

1ada3c01771712427cbad79d6ca0f552a9d549ab · ovidiucs · Nov 15, 2019 03:56

+145 −133 · 1 file changed

  • graph/db/sqltest.c +145 −133
diff --git a/graph/db/sqltest.c b/graph/db/sqltest.c
index 170dd39..dfeb7e2 100644
--- a/graph/db/sqltest.c
+++ b/graph/db/sqltest.c
@@ -10,16 +10,16 @@
 
   // sample test data
 char *sSql = "DROP TABLE IF EXISTS Friends;"
-            "CREATE TABLE Friends(Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER);"
-            "INSERT INTO Friends(Name,Age) VALUES ('Tom',10);"
-            "INSERT INTO Friends(Name,Age) VALUES ('Rebecca',32);"
-            "INSERT INTO Friends(Name,Age) VALUES ('Jim',34);"
-            "INSERT INTO Friends(Name,Age) VALUES ('Roger',12);"
-            "INSERT INTO Friends(Name,Age) VALUES ('Robert',16);";
+	    "CREATE TABLE Friends(Id INTEGER PRIMARY KEY, Name TEXT, Age INTEGER);"
+	    "INSERT INTO Friends(Name,Age) VALUES ('Tom',10);"
+	    "INSERT INTO Friends(Name,Age) VALUES ('Rebecca',32);"
+	    "INSERT INTO Friends(Name,Age) VALUES ('Jim',34);"
+	    "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 mainmenu(sqlite3 *db);
 int dbInsert(sqlite3 *db, char *data);
 int dbSelect(sqlite3 *db, char *data);
 int dbUpdate(sqlite3 *db, char *data);
@@ -31,7 +31,7 @@ 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
-  sqlite3 *db;
+  sqlite3 *db = NULL;
 
   // Return code - if the database is opened or created successfully SQLITE_OK is returned
   // otherwise an error is returned
@@ -47,128 +47,138 @@ int main(int argc, char **argv) {
       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");
-                }
+	  // 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");
+		fprintf(stdout,"Updated data succesfully\n");
     }
-          //if (!dbSelect(db,zSelect) ) {
-          //      fprintf(stdout,"Select data succesfully\n");
-          //      }
+	  //if (!dbSelect(db,zSelect) ) {
+	  //	  fprintf(stdout,"Select data succesfully\n");
+	  //	  }
 
-          // Close the database connection.
-          sqlite3_close(db);
+	  // Close the database connection.
+	  //sqlite3_close(db);
     } else {
       // Execute the main menu function
-            mainmenu();
+	    mainmenu(db);
     }
 
   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 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 mainmenu(sqlite3 *db) {
+	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[1024];
+	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("Enter query: >",stdout);
+				fgets(response, sizeof(response), stdin);
+				fputs("Calling dbSelect()...\n", stdout);
+				dbSelect(db, response);
+				break;
+			case 2:
+				fputs("dbDelete`\n",stdout);
+				break;
+			case 3:
+				fputs("dbUpdate()\n",stdout);
+				break;
+			case 4:
+				fputs("Option 4\n",stdout);
+				goto Exit;
+			default:
+				fputs("Please choose a valid menu option\n",stdout);
+	   
+			}
+		}
+	}
+Exit:
+	return 0;
 }
 
 int dbInsert(sqlite3 *db, char *data) {
@@ -189,11 +199,11 @@ int dbInsert(sqlite3 *db, char *data) {
   // 5th arg - error message writte (char **)
   int rc = sqlite3_exec(db,data,0,0,&zErrMsg);
   if ( rc != SQLITE_OK ) {
-         fprintf(stderr, "SQL error occured: %s\n", zErrMsg);
-         sqlite3_free(zErrMsg);
-         sqlite3_close(db);
+	 fprintf(stderr, "SQL error occured: %s\n", zErrMsg);
+	 sqlite3_free(zErrMsg);
+	 sqlite3_close(db);
 
-         return 1;
+	 return 1;
   }
 
   return 0;
@@ -222,22 +232,22 @@ int dbSelect(sqlite3 *db, char *data) {
   // int rc = sqlite3_prepare_v2(db,data,-1,&res,0);
 #if DEBUG == 1
   fprintf(stderr,"Database is: %s ; Data is: %s, ; Return Code is: %d ; stmt is %p\n",
-                  db,data,rc,res);
+		  db,data,rc,res);
 #endif
 
   rc = sqlite3_prepare_v2(db, data, -1, &res, 0);
 
 #if DEBUG == 1
   fprintf(stderr,"Database is: %s ; Data is: %s, ; Return Code is: %d ; stmt is %p\n",
-                  db,data,rc,res );
+		  db,data,rc,res );
 #endif
   // On success the sqlite3_prepare() returns SQLITE_OK
   if ( rc != SQLITE_OK ) {
-         fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
-         sqlite3_free(zErrMsg);
-         sqlite3_close(db);
+	 fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
+	 sqlite3_free(zErrMsg);
+	 sqlite3_close(db);
 
-         return 1;
+	 return 1;
   }
 
   //2.Step
@@ -245,41 +255,42 @@ int dbSelect(sqlite3 *db, char *data) {
   // returns integer - the result of the operation
   // Useful routines;
   rc = sqlite3_step(res);
-          for ( int i = 0; i < sqlite3_data_count(res) ; i++ ) {
-               fprintf(stdout,"| %s\t",sqlite3_column_origin_name(res,i));
-          }
-          fprintf(stdout,"\r\n");
+		  for ( int i = 0; i < sqlite3_data_count(res) ; i++ ) {
+	       fprintf(stdout,"| %s\t",sqlite3_column_origin_name(res,i));
+	  }
+	  fprintf(stdout,"\r\n");
   while ( rc == SQLITE_ROW ) {
-          int i = 0;
+	  int i = 0;
   // If the return code is SQLITE_ROW we can now iterate thorugh the entire column, row by row
     // - sqlite3_data_count() will return 0 if sqlite3_step was not called
-          for ( i = 0; i < sqlite3_data_count(res) ; i++ ) {
-               fprintf(stdout,"| %s\t",sqlite3_column_text(res,i));
-           }
-          fprintf(stdout,"\r\n");
+	  for ( i = 0; i < sqlite3_data_count(res) ; i++ ) {
+	       fprintf(stdout,"| %s\t",sqlite3_column_text(res,i));
+	   }
+	  fprintf(stdout,"\r\n");
   // Grab next row
-         rc = sqlite3_step(res);
+	 rc = sqlite3_step(res);
    }
   // Finalize - clean up after using.
   rc = sqlite3_finalize(res);
   if ( rc != SQLITE_OK ) {
-          fprintf(stderr,"SQL error occured on sqlite3_finalize(): %s with code: %d\n",zErrMsg, rc);
-          sqlite3_free(zErrMsg);
-          sqlite3_close(db);
+	  fprintf(stderr,"SQL error occured on sqlite3_finalize(): %s with code: %d\n",zErrMsg, rc);
+	  sqlite3_free(zErrMsg);
+	  sqlite3_close(db);
 
-          return 1;
+	  return 1;
   }
 
 
   return 0;
 }
 
+#if 0
 static int callback(void *data, int argc, char **argv, char **azColName ) {
 
 
   return 0;
 }
-
+#endif
 
 // Update data
 int dbUpdate (sqlite3 *db, char *data) {
@@ -296,14 +307,15 @@ int dbUpdate (sqlite3 *db, char *data) {
    // 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);
+	 fprintf(stderr, "SQL error occured: %s with code %d\n", zErrMsg, rc);
+	 sqlite3_free(zErrMsg);
+	 sqlite3_close(db);
 
-         return 1;
+	 return 1;
    }
-    rc = sqlite3_exec (db, data,callback,(void*) msg, &zErrMsg);
+//    rc = sqlite3_exec (db, data,callback,(void*) msg, &zErrMsg);
     fprintf(stdout,"%d\n",rc);
 
+   return 0;
 }
 // Delete data