Commit 19b53b91 by karade

Update generator.c

parent 74636791
Showing with 96 additions and 5 deletions
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <time.h>
#include "passenger_pool.h"
#include "generator.h"
......@@ -27,12 +28,19 @@ int main(void)
int residencyPoolSize = sizeof(residencies) / sizeof(char *);
int emailPoolSize = sizeof(emails) / sizeof(char *);
printf("\nEnter flight id to which flight to generate passengers:");
int flightId;
scanf("%d", &flightId);
printf("\nEnter how many passengers to generate:");
printf("\nEnter how many passengers to generate(0-72):");
int nameCount;
scanf("%d", &nameCount);
do
{
scanf("%d", &nameCount);
if(nameCount < 0 && nameCount > 73)
puts("Invalid number of passengers, Retry:");
}while(nameCount < 0 && nameCount > 73);
users data[SEATS] = {0};
......@@ -91,15 +99,98 @@ int main(void)
strcpy((data+i)->documentNum, docNum);
(data + i)->luggageClass = GetRand(0,3);
(data + i)->flight_id = flightId;
}
puts("Passengers generated, inserting into MySQL database...");
GenNames(data, nameCount);
MYSQL* con = connectToMySQLServer();//ühenduse loomine serveriga,
//tagastus on viit aadressile
for (i = 0; i < nameCount; i++)
{
insertPersonIntoTable(con, data[i]); //sisestatakse ükshaaval iga
} //genereeritud isik
puts("Passengers successfully inserted into MySQL server");
puts("Show database query? 1-yes 0-no");
int show;
scanf("%d", &show);
if (show == 1)
{
if (mysql_query(con, "SELECT * FROM Users"))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
PrintTable(result);
}
mysql_close(con);
return EXIT_SUCCESS;
}
void PrintTable(MYSQL_RES *result)
{
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
}
void insertPersonIntoTable(MYSQL* con, users data)
{
char query[1024];
sprintf(query, "INSERT INTO users (documentNum, firstName, "
"lastName, dateOfBirth, email, residency, checkedIn, seat,"
"luggageClass, flight_id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d);"
,data.documentNum, data.fName, data.lName, data.dateOfBirth,
data.email, data.residency, data.checkedIn, data.seat, data.luggageClass,
data.flight_id);
printf("DEBUG:%s\n\n", query);
if (mysql_query(con, query)) {
printf("Error inserting Users into MySQL table: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
}
MYSQL* connectToMySQLServer()
{
MYSQL* con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", "root", "Tarkvaraprojekt2023",
"Lennubroneering", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
return con;
}
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(EXIT_FAILURE);
}
void GenerateDateOfBirth(char *month, char *dateOB)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment