-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_setup.c
83 lines (75 loc) · 2.12 KB
/
db_setup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
for(int i = 0; i<argc; i++)
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
printf("\n");
return 0;
}
int setup_student(sqlite3 *db, char *errMsg)
{
int rc;
char *create_students_table =
"CREATE TABLE IF NOT EXISTS Students ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL,"
"age INTEGER,"
"email TEXT UNIQUE);";
rc = sqlite3_exec(db, create_students_table, callback, 0, &errMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s", errMsg);
sqlite3_free(errMsg);
return (0);
}
return (1);
}
int setup_subject(sqlite3 *db, char *errMsg)
{
int rc;
char *create_subjects_table =
"CREATE TABLE IF NOT EXISTS Subjects ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL UNIQUE);";
rc = sqlite3_exec(db, create_subjects_table, callback, 0, &errMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s", errMsg);
sqlite3_free(errMsg);
return (0);
}
return (1);
}
int setup_grades(sqlite3 *db, char *errMsg)
{
int rc;
char *create_grades_table =
"CREATE TABLE IF NOT EXISTS Grades ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"student_id INTEGER,"
"subject_id INTEGER,"
"grade REAL,"
"date TEXT,"
"FOREIGN KEY (student_id) REFERENCES Students(id) ON DELETE CASCADE,"
"FOREIGN KEY (subject_id) REFERENCES Subjects(id) ON DELETE CASCADE);";
rc = sqlite3_exec(db, create_grades_table, callback, 0, &errMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s", errMsg);
sqlite3_free(errMsg);
return (0);
}
return (1);
}
int setup_db(sqlite3 *db, char *errMsg)
{
if (!setup_student(db, errMsg))
return (0);
if (!setup_subject(db, errMsg))
return (0);
if (!setup_grades(db, errMsg))
return (0);
fprintf(stdout, "Tables created succesfully!\n");
return (1);
}