|
| 1 | +#include "sqlite3.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +// Prints data |
| 7 | +int callback(void *NotUsed, int argc, char **argv, char **ColName) |
| 8 | +{ |
| 9 | + for (int i = 0; i < argc; i++) |
| 10 | + { |
| 11 | + printf("%s = %s\n", ColName[i], argv[i] ? argv[i] : "NULL"); |
| 12 | + } |
| 13 | + return 0; |
| 14 | +} |
| 15 | + |
| 16 | +// Insert function to enter new data in database through the arguments |
| 17 | +int insert_data(sqlite3 *db, char *department, double credit_fee, double semester_fee, double other_fees, double admission_fee, int total_credit, int total_credit_below, int duration, int semester, int semester_below) |
| 18 | +{ |
| 19 | + char *err_msg = NULL; |
| 20 | + char sql[512]; |
| 21 | + |
| 22 | + sprintf(sql, "INSERT INTO D_CostingChart(Department, CreditFee, SemesterFee, OtherFees, AdmissionFee, TotalCredit, TotalCreditBelow, Duration, Semester, SemesterBelow) VALUES('%s', %.2f, %.2f, %.2f, %.2f, %d, %d, %d, %d, %d);", |
| 23 | + department, credit_fee, semester_fee, other_fees, admission_fee, total_credit, total_credit_below, duration, semester, semester_below); |
| 24 | + |
| 25 | + // Execute an sql statement in the database (to be simple here it inserts) |
| 26 | + int rc = sqlite3_exec(db, sql, 0, 0, &err_msg); |
| 27 | + |
| 28 | + if (rc != SQLITE_OK) |
| 29 | + { |
| 30 | + fprintf(stderr, "SQL error: %s\n", err_msg); |
| 31 | + sqlite3_free(err_msg); |
| 32 | + return 1; |
| 33 | + } |
| 34 | + |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +// Shows the whole table |
| 39 | +int show_table(sqlite3 *db) |
| 40 | +{ |
| 41 | + char *err_msg = NULL; |
| 42 | + char *sql = "SELECT * FROM D_CostingChart;"; |
| 43 | + |
| 44 | + // Execute an sql statement in the database (to be simple here it prints) |
| 45 | + int rc = sqlite3_exec(db, sql, callback, 0, &err_msg); |
| 46 | + |
| 47 | + if (rc != SQLITE_OK) |
| 48 | + { |
| 49 | + fprintf(stderr, "SQL error: %s\n", err_msg); |
| 50 | + sqlite3_free(err_msg); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +// Delete function to Delete any data in database through the argument |
| 58 | +int delete_data(sqlite3 *db, char *department) |
| 59 | +{ |
| 60 | + char *err_msg = NULL; |
| 61 | + char sql[256]; |
| 62 | + |
| 63 | + sprintf(sql, "DELETE FROM D_CostingChart WHERE Department='%s';", department); |
| 64 | + |
| 65 | + // Execute an sql statement in the database (to be simple here it deletes) |
| 66 | + int rc = sqlite3_exec(db, sql, 0, 0, &err_msg); |
| 67 | + |
| 68 | + if (rc != SQLITE_OK) |
| 69 | + { |
| 70 | + fprintf(stderr, "SQL error: %s\n", err_msg); |
| 71 | + sqlite3_free(err_msg); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +// Update function to modify data in database through the arguments |
| 79 | +int update_data(sqlite3 *db, char *department, double credit_fee, double semester_fee, double other_fees, double admission_fee, int total_credit, int total_credit_below, int duration, int semester, int semester_below) |
| 80 | +{ |
| 81 | + char *err_msg = NULL; |
| 82 | + char sql[512]; |
| 83 | + |
| 84 | + sprintf(sql, "UPDATE D_CostingChart SET CreditFee=%.2f, SemesterFee=%.2f, OtherFees=%.2f, AdmissionFee=%.2f, TotalCredit=%d, TotalCreditBelow=%d, Duration=%d, Semester=%d, SemesterBelow=%d WHERE Department='%s';", |
| 85 | + credit_fee, semester_fee, other_fees, admission_fee, total_credit, total_credit_below, duration, semester, semester_below, department); |
| 86 | + |
| 87 | + // Execute an sql statement in the database (to be simple here it updates) |
| 88 | + int rc = sqlite3_exec(db, sql, 0, 0, &err_msg); |
| 89 | + |
| 90 | + if (rc != SQLITE_OK) |
| 91 | + { |
| 92 | + fprintf(stderr, "SQL error: %s\n", err_msg); |
| 93 | + sqlite3_free(err_msg); |
| 94 | + return 1; |
| 95 | + } |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +// Total cost calculation function that was initially practiced here before implementing in test.c |
| 101 | +int calculate_total_cost(sqlite3 *db, char *department) |
| 102 | +{ |
| 103 | + char sql[256]; |
| 104 | + sqlite3_stmt *stmt; |
| 105 | + |
| 106 | + sprintf(sql, "SELECT CreditFee, SemesterFee, OtherFees, AdmissionFee, TotalCredit, TotalCreditBelow, Semester, SemesterBelow FROM D_CostingChart WHERE Department='%s';", department); |
| 107 | + |
| 108 | + int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); |
| 109 | + |
| 110 | + if (rc != SQLITE_OK) |
| 111 | + { |
| 112 | + fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db)); |
| 113 | + return 1; |
| 114 | + } |
| 115 | + |
| 116 | + if (sqlite3_step(stmt) == SQLITE_ROW) |
| 117 | + { |
| 118 | + double credit_fee = sqlite3_column_double(stmt, 0); |
| 119 | + double semester_fee = sqlite3_column_double(stmt, 1); |
| 120 | + double other_fees = sqlite3_column_double(stmt, 2); |
| 121 | + double admission_fee = sqlite3_column_double(stmt, 3); |
| 122 | + int total_credit = sqlite3_column_int(stmt, 4); |
| 123 | + int semester = sqlite3_column_int(stmt, 6); |
| 124 | + |
| 125 | + double total_cost = (credit_fee * total_credit) + (semester_fee * semester) + admission_fee + other_fees; |
| 126 | + printf("Total cost for %s: %.2f\n", department, total_cost); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + printf("Department not found.\n"); |
| 131 | + } |
| 132 | + |
| 133 | + sqlite3_finalize(stmt); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +int main(int argc, char **argv) |
| 139 | +{ |
| 140 | + sqlite3 *db; |
| 141 | + char *err_msg = NULL; |
| 142 | + |
| 143 | + int rc = sqlite3_open("d_costing_chart.db", &db); |
| 144 | + |
| 145 | + if (rc != SQLITE_OK) |
| 146 | + { |
| 147 | + fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); |
| 148 | + sqlite3_close(db); |
| 149 | + |
| 150 | + return 1; |
| 151 | + } |
| 152 | + |
| 153 | + char *sql = "CREATE TABLE IF NOT EXISTS D_CostingChart(Department TEXT PRIMARY KEY, CreditFee REAL, SemesterFee REAL, OtherFees REAL, AdmissionFee REAL, TotalCredit INTEGER, TotalCreditBelow INTEGER, Duration INTEGER, Semester INTEGER, SemesterBelow INTEGER);"; |
| 154 | + |
| 155 | + rc = sqlite3_exec(db, sql, 0, 0, &err_msg); |
| 156 | + |
| 157 | + if (rc != SQLITE_OK) |
| 158 | + { |
| 159 | + fprintf(stderr, "SQL error: %s\n", err_msg); |
| 160 | + |
| 161 | + sqlite3_free(err_msg); |
| 162 | + sqlite3_close(db); |
| 163 | + |
| 164 | + return 1; |
| 165 | + } |
| 166 | + |
| 167 | + if (argc > 1 && strcmp(argv[1], "-s") == 0) |
| 168 | + { |
| 169 | + show_table(db); |
| 170 | + } |
| 171 | + |
| 172 | + if (argc > 1 && strcmp(argv[1], "-i") == 0) |
| 173 | + { |
| 174 | + if (argc != 12) |
| 175 | + { |
| 176 | + printf("Usage: %s -i <department> <credit_fee> <semester_fee> <other_fees> <admission_fee> <total_credit> <total_credit_below> <duration> <semester> <semester_below>\n", argv[0]); |
| 177 | + return 1; |
| 178 | + } |
| 179 | + |
| 180 | + insert_data(db, argv[2], atof(argv[3]), atof(argv[4]), atof(argv[5]), atof(argv[6]), atoi(argv[7]), atoi(argv[8]), atoi(argv[9]), atoi(argv[10]), atoi(argv[11])); |
| 181 | + } |
| 182 | + |
| 183 | + if (argc > 1 && strcmp(argv[1], "-d") == 0) |
| 184 | + { |
| 185 | + if (argc != 3) |
| 186 | + { |
| 187 | + printf("Usage: %s -d <department>\n", argv[0]); |
| 188 | + return 1; |
| 189 | + } |
| 190 | + |
| 191 | + delete_data(db, argv[2]); |
| 192 | + } |
| 193 | + |
| 194 | + if (argc > 1 && strcmp(argv[1], "-u") == 0) |
| 195 | + { |
| 196 | + if (argc != 12) |
| 197 | + { |
| 198 | + printf("Usage: %s -u <department> <credit_fee> <semester_fee> <other_fees> <admission_fee> <total_credit> <total_credit_below> <duration> <semester> <semester_below>\n", argv[0]); |
| 199 | + return 1; |
| 200 | + } |
| 201 | + |
| 202 | + update_data(db, argv[2], atof(argv[3]), atof(argv[4]), atof(argv[5]), atof(argv[6]), atoi(argv[7]), atoi(argv[8]), atoi(argv[9]), atoi(argv[10]), atoi(argv[11])); |
| 203 | + } |
| 204 | + |
| 205 | + if (argc > 1 && strcmp(argv[1], "-c") == 0) |
| 206 | + { |
| 207 | + if (argc != 3) |
| 208 | + { |
| 209 | + printf("Usage: %s -c <department>\n", argv[0]); |
| 210 | + return 1; |
| 211 | + } |
| 212 | + |
| 213 | + calculate_total_cost(db, argv[2]); |
| 214 | + } |
| 215 | + |
| 216 | + sqlite3_close(db); |
| 217 | + |
| 218 | + return 0; |
| 219 | +} |
0 commit comments