How to do it…

  1. Initialize a MYSQL object:
conn = mysql_init(NULL);
  1. Establish a connection to the MySQL server running at the localhost. Also, connect to the database that you want to work on:
mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)
  1. Enter the information of the new row that you want to insert into the users table in the ecommerce database, which will be for the new user's email address, password, and address of delivery:
printf("Enter email address: ");
scanf("%s", emailaddress);
printf("Enter password: ");
scanf("%s", upassword);
printf("Enter address of delivery: ");
getchar();
gets(deliveryaddress);
  1. Prepare an SQL INSERT statement comprising this information; that is, the email address, password, and address of delivery of the new user:
strcpy(sqlquery,"INSERT INTO users(email_address, password, address_of_delivery)VALUES (\'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\', \'");
strcat(sqlquery,upassword);
strcat(sqlquery,"\', \'");
strcat(sqlquery,deliveryaddress);
strcat(sqlquery,"\')");
  1. Execute the SQL INSERT statement to insert a new row into the users table in the ecommerce database:
mysql_query(conn, sqlquery)
  1. Close the connection handler:
mysql_close(conn);

The adduser.c program for inserting a row into a MySQL database table is shown in the following code:

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

void main() {
MYSQL *conn;
char *server = "127.0.0.1";
char *user = "root";
char *password = "Bintu2018$";
char *database = "ecommerce";
char emailaddress[30],
upassword[30],deliveryaddress[255],sqlquery[255];
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server, user, password, database, 0,
NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
printf("Enter email address: ");
scanf("%s", emailaddress);
printf("Enter password: ");
scanf("%s", upassword);
printf("Enter address of delivery: ");
getchar();
gets(deliveryaddress);
strcpy(sqlquery,"INSERT INTO users(email_address, password,
address_of_delivery)VALUES (\'");
strcat(sqlquery,emailaddress);
strcat(sqlquery,"\', \'");
strcat(sqlquery,upassword);
strcat(sqlquery,"\', \'");
strcat(sqlquery,deliveryaddress);
strcat(sqlquery,"\')");
if (mysql_query(conn, sqlquery) != 0)
{
fprintf(stderr, "Row could not be inserted into users
table\n");
exit(1);
}
printf("Row is inserted successfully in users table\n");
mysql_close(conn);
}

Now, let's go behind the scenes to understand the code better.