-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit.c
More file actions
58 lines (54 loc) · 1.74 KB
/
Copy pathsubmit.c
File metadata and controls
58 lines (54 loc) · 1.74 KB
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
/*
* Program: submit.c
* Name: Joshua Johnston, Morgan McEntire
* Date: 4/27/2016
* Purpose: Reads in MACs from log file and updates database
* Assumptions: log file is stored at absences.log, rollcall executable has been run
*/
#include "rollcall.h"
int main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char command[COMMAND_SIZE];
conn = mysql_init (NULL);
if (conn == NULL){
fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
exit (1);
}
//conncet to database
if (mysql_real_connect (
conn, //Pointer to connection handler
def_host_name, //Host to connect to
def_user_name, //User name
def_password, //Password
def_db_name, //Database to use
0, //Port (use default)
NULL, //Socket (use default)
0) //Flags (none)
== NULL)
{
fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n", mysql_errno (conn), mysql_error (conn));
exit (1);
}
fp = fopen("absences.log", "r");
if (fp == NULL)
exit(2);
//read in log file line by line and update database
while ((read = getline(&line, &len, fp)) != -1) {
line[17] = '\0'; //put null at end of string
snprintf(command, COMMAND_SIZE, "UPDATE student SET absences = absences + 1 WHERE mac='%s'", line);
if (mysql_query (conn, command) != 0){
printf("mysql_query() failed\n");
}else{
printf("Student marked absent\n");
}
}
fclose(fp);
mysql_close (conn);
if (line)
free(line);
exit(0);
}