GeekInterview.com
Series: Subject: Topic:
Question: 20 of 267

Adding user from c program to Mysql.

Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.
Asked by: Interview Candidate | Asked on: Nov 10th, 2011
Showing Answers 1 - 1 of 1 Answers
yuvraj

Answered On : Dec 9th, 2011

This is program to add the add the user in database with privilege to SELECT only

Code
  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int main()
  6. {
  7.  
  8. char name[10];
  9. char password[10];
  10. char database[10];
  11. char addUser[60]="";
  12. char grantACC[60]="";
  13.  
  14. // initialize the variables within
  15. MYSQL *connection=mysql_init(NULL);             //MYSQL is structure represents a handle to one database connection
  16.  
  17.  
  18. // connect to the database
  19.     if (!mysql_real_connect(connection,"localhost","root","mysql","clanguage", 0, NULL, 0)) {
  20.             printf("in the if block");
  21.             printf("Conection error : %s
  22. ", mysql_error(connection));            //For print error if connection is not stablished
  23.             exit(1);
  24.     }
  25.  
  26.  printf("connection established successfully
  27. ");
  28.  
  29. //retrieve User name from STDIN
  30.         printf("Enter the User Name
  31. ");
  32.         scanf("%s",name);
  33.  
  34. //retrieve Password from STDIN
  35.         printf("Enter the Password
  36. ");
  37.         scanf("%s",password);
  38.  
  39. //retrieve database name from STDIN
  40.         printf("Enter the Database Name
  41. ");
  42.         scanf("%s",database);
  43.  
  44. //creating SQL query in the form String
  45.         strcat(addUser,"create user ");
  46.         strcat(addUser,name);
  47.         strcat(addUser,"@localhost identified by ");
  48.         strcat(addUser,password);
  49.         strcat(addUser,"");
  50. //execute the SQL query {create user <user name>@localhost identified by <password>}
  51.         mysql_query(connection,addUser);
  52. //puts(addUser);
  53.  
  54.  
  55. //creating another SQL query in the String
  56.         strcat(grantACC,"grant SELECT on ");
  57.         strcat(grantACC,database);
  58.         strcat(grantACC,".* to ");
  59.         strcat(grantACC,name);
  60.         strcat(grantACC,"@localhost");
  61. //execute the SQL query { grant SELECT on <database name>.* to <user name>@localhost }
  62.         mysql_query(connection,grantACC);
  63. //puts(grantACC);
  64.  
  65.  
  66. //closing the connection
  67.         mysql_close(connection);
  68.  
  69.         printf("User has been created Successfully
  70. ");
  71.  
  72. }

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.