-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
199 lines (182 loc) · 6.18 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdlib.h>
#include <stdio.h>
#include "exerciser.h"
#include<string.h>
int main (int argc, char *argv[])
{
//Allocate & initialize a MYSQL object
MYSQL *conn = mysql_init(NULL);
if (conn == NULL) {
fprintf(stderr, "%s\n", mysql_error(conn));
return 1;
}
//Establish a connection to the database
//Parameters: connection handler, host name, user name, password,
// database name, port numbrer, unix socket, client flag
if (mysql_real_connect(conn, "localhost", "myuser", "passw0rd", "ACC_BBALL", 0, NULL, 0) == NULL) {
exit_with_error(conn);
}
//TODO: create PLAYER, TEAM, STATE, and COLOR tables in the ACC_BBALL database
// load each table with rows from the provided source txt files
if (mysql_query(conn, "DROP TABLE IF EXISTS PLAYER")) {
exit_with_error(conn);
}
if (mysql_query(conn, "CREATE TABLE PLAYER(PLAYER_ID INT NOT NULL AUTO_INCREMENT, TEAM_ID INT, UNIFORM_NUM INT, FIRST_NAME TEXT, LAST_NAME TEXT, MPG INT, PPG INT, RPG INT, APG INT, SPG FLOAT, BPG FLOAT,PRIMARY KEY (PLAYER_ID))")) {
exit_with_error(conn);
}
if (mysql_query(conn, "DROP TABLE IF EXISTS TEAM")) {
exit_with_error(conn);
}
if (mysql_query(conn, "CREATE TABLE TEAM (TEAM_ID INT NOT NULL AUTO_INCREMENT, NAME TEXT, STATE_ID INT, COLOR_ID INT, WINS INT, LOSSES INT,PRIMARY KEY (TEAM_ID))")) {
exit_with_error(conn);
}
if (mysql_query(conn, "DROP TABLE IF EXISTS STATE")) {
exit_with_error(conn);
}
if (mysql_query(conn, "CREATE TABLE STATE (STATE_ID INT NOT NULL AUTO_INCREMENT, NAME TEXT,PRIMARY KEY (STATE_ID))")) {
exit_with_error(conn);
}
if (mysql_query(conn, "DROP TABLE IF EXISTS COLOR")) {
exit_with_error(conn);
}
if (mysql_query(conn, "CREATE TABLE COLOR (COLOR_ID INT NOT NULL AUTO_INCREMENT, NAME TEXT,PRIMARY KEY (COLOR_ID))")) {
exit_with_error(conn);
}
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
const char s[2] = " ";
fp = fopen("player.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
//printf("%s", line);
if(read > 1){
char *curr;
char input[1000] = "INSERT INTO PLAYER VALUES(";
curr = strtok(line, s);
int PLAYER_ID = atoi(curr);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int TEAM_ID = atoi(curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int UNIFORM_NUM = atoi(curr);
curr = strtok(NULL, s);
strcat(input,"'");strcat(input,curr);strcat(input,"'");strcat(input,",");
char FIRST_NAME[20];
strcpy(FIRST_NAME,curr);
curr = strtok(NULL, s);
strcat(input,"'");strcat(input,curr);strcat(input,"'");strcat(input,",");
char LAST_NAME[20];
strcpy(LAST_NAME,curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int MPG = atoi(curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int PPG = atoi(curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int RPG = atoi(curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
int APG = atoi(curr);
curr = strtok(NULL, s);
strcat(input,curr);strcat(input,",");
double SPG = strtof(curr,NULL);
curr = strtok(NULL, "\n");
strcat(input,curr);strcat(input,")");
double BPG = strtof(curr,NULL);
//printf("%s\n",input);
add_player(conn, TEAM_ID, UNIFORM_NUM, FIRST_NAME, LAST_NAME, MPG, PPG,RPG, APG, SPG, BPG);
//printf("PlayerID, %d, TEAM_ID, %d, name, %s %s ,SPG,%f,BPG,%f\n",PLAYER_ID,TEAM_ID, FIRST_NAME,LAST_NAME, SPG,BPG);
//if (mysql_query(conn, input)) {
// exit_with_error(conn);
//}
}
}
fclose(fp);
fp = fopen("team.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
if(read > 1){
char *curr;
char input[1000] = "INSERT INTO TEAM VALUES(";
curr = strtok(line, s);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, s);
strcat(input,"'");strcat(input,curr);strcat(input,"'");strcat(input,",");
char name[20];
strcpy(name,curr);
curr = strtok(NULL, s);
int state_id = atoi(curr);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, s);
int color_id = atoi(curr);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, s);
int wins = atoi(curr);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, "\n");
int losses = atoi(curr);
strcat(input,curr);strcat(input,")");
//printf("%s\n",input);
add_team(conn,name,state_id,color_id,wins,losses);
//if (mysql_query(conn, input)) {
// exit_with_error(conn);
//}
}
}
fclose(fp);
fp = fopen("state.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
if(read > 1){
char *curr;
char input[1000] = "INSERT INTO STATE VALUES(";
curr = strtok(line, s);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, "\n");
char name[20];
strcpy(name,curr);
strcat(input,"'");strcat(input,curr);strcat(input,"'");strcat(input,")");
//printf("%s\n",input);
add_state(conn,name);
//if (mysql_query(conn, input)) {
// exit_with_error(conn);
//}
}
}
fclose(fp);
fp = fopen("color.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
if(read > 1){
char *curr;
char input[1000] = "INSERT INTO COLOR VALUES(";
curr = strtok(line, s);
strcat(input,curr);strcat(input,",");
curr = strtok(NULL, "\n");
char name[20];
strcpy(name,curr);
strcat(input,"'");strcat(input,curr);strcat(input,"'");strcat(input,")");
//printf("%s\n",input);
add_color(conn,name);
//if (mysql_query(conn, input)) {
// exit_with_error(conn);
//}
}
}
fclose(fp);
exercise(conn);
//Close database connection
mysql_close(conn);
return 0;
}