0% found this document useful (0 votes)
20 views

Csprog 2 Workingna

This C program connects to a MySQL database and provides functions to perform CRUD (create, read, update, delete) operations on student records stored in a database table. The main function initializes the MySQL connection and presents a menu for the user to add, edit, delete, search, or list student records by calling the corresponding functions. These functions construct and execute the necessary SQL queries to perform the requested operation.

Uploaded by

Aaron Coronia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Csprog 2 Workingna

This C program connects to a MySQL database and provides functions to perform CRUD (create, read, update, delete) operations on student records stored in a database table. The main function initializes the MySQL connection and presents a menu for the user to add, edit, delete, search, or list student records by calling the corresponding functions. These functions construct and execute the necessary SQL queries to perform the requested operation.

Uploaded by

Aaron Coronia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <mysql.h>
#include <string.h>

void add_record(MYSQL *conn);


void edit_record(MYSQL *conn);
void delete_record(MYSQL *conn);
void search_record(MYSQL *conn);
void list_records(MYSQL *conn);

int main()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int choice = 0;

conn = mysql_init(NULL);

if (!mysql_real_connect(conn, "localhost", "root", NULL, "database0", 0, NULL,


0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

while (choice != 6) {
printf("\n1. Add record\n");
printf("2. Edit record\n");
printf("3. Delete record\n");
printf("4. Search record\n");
printf("5. List records\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
add_record(conn);
break;
case 2:
edit_record(conn);
break;
case 3:
delete_record(conn);
break;
case 4:
search_record(conn);
break;
case 5:
list_records(conn);
break;
case 6:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break; // added break to default case
}
}

mysql_close(conn);

return 0;
}

void add_record(MYSQL *conn)


{
char query[1024];
char studname[50], course[50], year[5], address[100], sex[10], dob[12],
contact[20], email[50];
int ret;

printf("\nEnter student name: ");


scanf("%49s", studname); // limit input to 49 characters to avoid buffer
overflow
printf("Enter course: ");
scanf("%49s", course);
printf("Enter year: ");
scanf("%4s", year);
printf("Enter address: ");
scanf("%99s", address);
printf("Enter sex: ");
scanf("%9s", sex);
printf("Enter date of birth (YYYY-MM-DD): ");
scanf("%11s", dob);
printf("Enter contact number: ");
scanf("%19s", contact);
printf("Enter email address: ");
scanf("%49s", email);
sprintf(query, "INSERT INTO students (name, course, year, address, sex, dob,
contact, email) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", studname,
course, year, address, sex, dob, contact, email);
ret = mysql_query(conn, query);

if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
} else {
printf("Record added successfully!\n");
}
}

void edit_record(MYSQL *conn)


{
char query[1024];
char studname[50], course[50], year[5], address[100], sex[10], dob[12],
contact[20], email[50];
int id, ret;
printf("\nEnter student id to edit: ");
scanf("%d", &id);

sprintf(query, "SELECT * FROM students WHERE id = %d", id);

ret = mysql_query(conn, query);

if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
return;
}

MYSQL_RES *result = mysql_store_result(conn);


MYSQL_ROW row = mysql_fetch_row(result);

if (row == NULL) {
printf("Record not found!\n");
return;
}

printf("\nEnter new student name: ");


scanf("%49s", studname); // limit input to 49 characters to avoid buffer overflow
printf("Enter new course: ");
scanf("%49s", course);
printf("Enter new year: ");
scanf("%4s", year);
printf("Enter new address: ");
scanf("%99s", address);
printf("Enter new sex: ");
scanf("%9s", sex);
printf("Enter new date of birth (YYYY-MM-DD): ");
scanf("%11s", dob);
printf("Enter new contact number: ");
scanf("%19s", contact);
printf("Enter new email address: ");
scanf("%49s", email);

sprintf(query, "UPDATE students SET name='%s', course='%s', year='%s',


address='%s', sex='%s', dob='%s', contact='%s', email='%s' WHERE id=%d",
studname, course, year, address, sex, dob, contact, email, id);

ret = mysql_query(conn, query);

if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
} else {
printf("Record updated successfully!\n");
}

mysql_free_result(result);
}
void delete_record(MYSQL *conn)
{
char query[1024];
int id, ret;
printf("\nEnter student id to delete: ");
scanf("%d", &id);
sprintf(query, "DELETE FROM students WHERE id=%d", id);
ret = mysql_query(conn, query);
if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
} else {
printf("Record deleted successfully!\n");
}
}

void search_record(MYSQL *conn)


{
char query[1024];
char keyword[50];
int ret;
printf("\nEnter keyword to search: ");
scanf("%49s", keyword); // limit input to 49 characters to avoid buffer
overflow
sprintf(query, "SELECT * FROM students WHERE name LIKE '%%%s%%' OR course LIKE
'%%%s%%' OR year LIKE '%%%s%%' OR address LIKE '%%%s%%' OR sex LIKE '%%%s%%' OR dob
LIKE '%%%s%%' OR contact LIKE '%%%s%%' OR email LIKE '%%%s%%'",
keyword, keyword, keyword, keyword, keyword, keyword, keyword,
keyword);
ret = mysql_query(conn, query);
if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
return;
}
MYSQL_RES *result = mysql_store_result(conn);
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
int i = 0;
while (i < num_fields) {
printf("%s\t", row[i] ? row[i] : "NULL");
i++;
}
printf("\n");
}
mysql_free_result(result);
}

void list_records(MYSQL *conn)


{
char query[1024];
int ret;
sprintf(query, "SELECT * FROM students");
ret = mysql_query(conn, query);
if (ret != 0) {
fprintf(stderr, "Error: %s\n", mysql_error(conn));
return;
}
MYSQL_RES *result = mysql_store_result(conn);
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
int i = 0;
while (i < num_fields) {
printf("%s\t", row[i] ? row[i] : "NULL");
i++;
}
printf("\n");
}
mysql_free_result(result);
}

You might also like