Alright I'm doing a basic inventory program. I have 5 options and its to view different video games (very simple).
I'm new to C coding, but everytime I try to run look up an item by UPC , after I type in the UPC it gives me a segementation fault and I can't find where the fault is coming from. Here is my setup for the MySQL database
DATABASE:
Code:
CREATE TABLE BARCODE_SCAN
(
BARCODE_ID VARCHAR(6) NOT NULL,
GAME_TITLE VARCHAR(20) NOT NULL,
DEVELOPER VARCHAR(20) NOT NULL,
PRICE DECIMAL(4,2) NOT NULL,
STOCK_VALUE INT(20) NOT NULL,
PRIMARY KEY (BARCODE_ID)
);
INSERT INTO BARCODE_SCAN
VALUES ('123456', 'Call of Duty', 'Activision', '59.99', '12');
INSERT INTO BARCODE_SCAN
VALUES ('234567', 'Halo 5', 'Bungie', '59.99', '15');
INSERT INTO BARCODE_SCAN
VALUES ('345678', 'World of Warcraft', 'Blizzard', '15.99', '5');
INSERT INTO BARCODE_SCAN
VALUES ('456789', 'Mine Craft', 'Mojang', '19.99', '15');
INSERT INTO BARCODE_SCAN
VALUES ('987654', 'Final Fantasy VIII', 'Square Soft', '19.99', '10');
INSERT INTO BARCODE_SCAN
VALUES ('876543', 'GTA 5', 'Rockstar', '59.99', '1');
INSERT INTO BARCODE_SCAN
VALUES ('765432', 'Diablo 3', 'Blizzard', '59.99', '15');
INSERT INTO BARCODE_SCAN
VALUES ('654321', 'Dead Island', 'Tech land', '49.99', '69');
INSERT INTO BARCODE_SCAN
VALUES ('123123', 'Resident Evil', 'Capcom', '39.99', '15');
INSERT INTO BARCODE_SCAN
VALUES ('234234', 'Zelda', 'Nintendo', '49.99', '15');
Here is my code. Option 1 works fine, its option 2 that doesn't and I haven't even attempted option 3 or 4 (adding and taking away quantity amounts).
************************************************** *************************************
Code:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
int main()
{
MYSQL *conn;
MYSQL_ROW row;
MYSQL_FIELD *field;
MYSQL_RES *result;
int input_scan;
char *server = "localhost";
char *user = "root";
char *password = "1"; /* set me first */
char *database = "PROJECT";
char query[100];
int i;
int choice;
do
{
system("clear");
printf("Welcome to Jason Chea & Myles Hattabaugh Project\n\n");
printf("Please make a choice:\n"
"1) View Complete Inventory \n"
"2) Item Description \n"
"3) Add Item \n"
"4) Remove Item \n"
"5) Exit \n");
scanf("%d", &choice);
if (choice == 1)
{
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "SELECT GAME_TITLE FROM BARCODE_SCAN"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
result = mysql_use_result(conn);
/* output table name */
printf("Inventory:\n\n");
while ((row = mysql_fetch_row(result)) != NULL)
{
printf("%s \n", row[0]);
}
if(mysql_query(conn, "SELECT * FROM BARCODE_SCAN"))
{
mysql_error(conn);
}
result = mysql_store_result(conn);
printf("\n\n");
if (result == NULL)
{
mysql_error(conn);
}
exit(0);
}
else if (choice == 2)
{
printf("Enter or Scan a UPC Code:");
scanf("%i", &input_scan);
sprintf(query, "SELECT * FROM BARCODE_SCAN WHERE BARCODE_ID LIKE ('%i')", input_scan);
mysql_query(conn,query);
{
mysql_error(conn);
}
result = mysql_store_result(conn);
if (result == NULL)
{
mysql_error(conn);
}
int num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
}
else if (choice == 3)
{
printf("option not available yet");
}
else if (choice == 4)
{
printf("option not available yet");
}
else
{
printf("Invalid Choice");
}
}
while (choice !=5);
/* close connection */
mysql_free_result(result);
mysql_close(conn);
exit(0);
}
Any tips would be greatly appreciated. Thanks!