GeekInterview.com
Series: Subject: Topic:
Question: 1 of 58

Write a C/C++ program to add a user to MySQL.

The user should be permitted to only "SELECT" entries from the given database. What resources did you use to do research for this project? What development tools did you do use to complete the project? How did you test & debug the project? Why have you chosen to use the programming language used in the project?
Asked by: purushottam77 | Member Since Feb-2012 | Asked on: Feb 19th, 2012

View all questions by purushottam77

Showing Answers 1 - 2 of 2 Answers
vishnu raju

Answered On : Mar 13th, 2012

try this :)

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. }

Yes  1 User has rated as useful.
  
Login to rate this answer.
sumanth.boss

Answered On : May 26th, 2012

View all answers by sumanth.boss

Dude, i tried to execute it . it execute successfully . but the grantACC is not getting executed . after adding user and trying to login him , it is saying error 1045(28000)

  
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.