-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_db.c
93 lines (71 loc) · 2.38 KB
/
clean_db.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
84
85
86
87
88
89
90
91
92
93
#include <sqlite3.h>
#include <stdio.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
int i;
for(i = 0; i<argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
}
int main(){
sqlite3 *db;
int rc;
char *zErrMsg;
rc = sqlite3_open("hospitium.db", &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}
char* createTablePatients = "DROP TABLE IF EXISTS Patients; CREATE TABLE Patients(" \
"id INTEGER PRIMARY KEY NOT NULL," \
"age INT NOT NULL," \
"room INT NOT NULL," \
"doctor INT NOT NULL," \
"name CHAR(50) NOT NULL," \
"gender CHAR(10) NOT NULL," \
"diagnosis CHAR(100) NOT NULL);";
rc = sqlite3_exec(db, createTablePatients, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Patients table created successfully\n");
}
char* createTableDoctors = "DROP TABLE IF EXISTS Doctors; CREATE TABLE Doctors(" \
"id INTEGER PRIMARY KEY NOT NULL," \
"age INT NOT NULL," \
"department INT NOT NULL," \
"name CHAR(50) NOT NULL," \
"gender CHAR(10) NOT NULL);";
rc = sqlite3_exec(db, createTableDoctors, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Doctors table created successfully\n");
}
char* createTableRooms = "DROP TABLE IF EXISTS Rooms; CREATE TABLE Rooms(" \
"no INT NOT NULL," \
"capacity INT NOT NULL);";
rc = sqlite3_exec(db, createTableRooms, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Rooms table created successfully\n");
}
char* createTableDepartments = "DROP TABLE IF EXISTS Departments; CREATE TABLE Departments(" \
"id INTEGER PRIMARY KEY NOT NULL," \
"title CHAR(50) NOT NULL);";
rc = sqlite3_exec(db, createTableDepartments, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
} else {
fprintf(stdout, "Departments table created successfully\n");
}
sqlite3_close(db);
return 0;
}