Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why do I fail making a database and tables? #define _ CRT _ SECURE _ NO _ WARNINGS #include #include #include mysql . h

Why do I fail making a database and tables?
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include "mysql.h"
#pragma comment(lib, "libmysql.lib")
const char* host = "localhost"; // change if necessary
const char* user = "root"; // change if necessary
const char* pw = "dltjgus43"; // change if necessary
const char* db = "project2";
#define MAX_LEN 13000
void queryType1(MYSQL* conn);
void queryType2(MYSQL* conn);
void queryType3(MYSQL* conn);
void queryType4(MYSQL* conn);
void queryType5(MYSQL* conn);
void queryType6(MYSQL* conn);
void queryType7(MYSQL* conn);
void queryResult(MYSQL* conn, const char* query){
if (mysql_query(conn, query)){
printf("Query failed: %s
", mysql_error(conn));
return;
}
MYSQL_RES* result = mysql_store_result(conn);
if (result == NULL){
printf("Failed to retrieve result set: %s
", mysql_error(conn));
return;
}
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))){
for (int i =0; i < num_fields; ++i){
if (row[i]!= NULL){
printf("%s ", row[i]);
}
else {
printf("NULL ");
}
}
printf("
");
}
mysql_free_result(result);
}
int main(void){
MYSQL* connection = NULL;
MYSQL conn;
MYSQL_RES* sql_result;
MYSQL_ROW sql_row;
FILE* fp = fopen("CRUD.txt","rt");; // open CRUD file.
char line[MAX_LEN];
int state =0;
int enter;
if (fp == NULL){
fprintf(stderr, "open file error!");
exit(0);
}
if (mysql_init(&conn)== NULL){
printf("mysql_init() error!");
return 1;
}
connection = mysql_real_connect(&conn, host, user, pw, db,3306,(const char*)NULL,0);
if (connection == NULL){
printf("%d ERROR : %s
", mysql_errno(&conn), mysql_error(&conn));
return 1;
}
else {
printf("Connection Succeeded
");
while (fgets(line, sizeof(line), fp)!= NULL){
if (!strcmp(line,"$$$
"))// read lines from CRUD file, '$$$' separates CREATE / DELETE parts.
break;
mysql_query(connection, line);
}
if (mysql_select_db(&conn, "project2")){
printf("%d ERROR : %s
", mysql_errno(&conn), mysql_error(&conn));
return 1;
}
const char* query = "SELECT * FROM customer";
state = mysql_query(connection, query);
if (state ==0){
sql_result = mysql_store_result(connection);
printf("[ SELECT * FROM CUSTOMER ]
");
while ((sql_row = mysql_fetch_row(sql_result))!= NULL)
printf("%s %s %s %s
", sql_row[0], sql_row[1], sql_row[2], sql_row[3]);
mysql_free_result(sql_result);
}
// comment out if you want to persist example db instance.
while (fgets(line, sizeof(line), fp)!= NULL){
mysql_query(connection, line); // these are DELETEs & DROPs.
}
fclose(fp);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions

Question

Challenges Facing Todays Organizations?

Answered: 1 week ago