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.

Showing Answers 1 - 1 of 1 Answers

yuvraj

  • 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. """";

  6.  

  7. // initialize the variables within//MYSQL is structure represents a handle to one database connection

  8.  

  9.  

  10. // connect to the database"localhost","root","mysql","clanguage""in the if block""Conection error : %s

  11. ", mysql_error(connection));            //For print error if connection is not stablished"connection established successfully

  12. ");

  13.  

  14. //retrieve User name from STDIN"Enter the User Name

  15. ");

  16.         scanf("%s",name);

  17.  

  18. //retrieve Password from STDIN"Enter the Password

  19. ");

  20.         scanf("%s",password);

  21.  

  22. //retrieve database name from STDIN"Enter the Database Name

  23. ");

  24.         scanf("%s",database);

  25.  

  26. //creating SQL query in the form String

  27.         strcat(addUser,"create user ");

  28.         strcat(addUser,name);

  29.         strcat(addUser,"@localhost identified by ");

  30.         strcat(addUser,password);

  31.         strcat(addUser,"");

  32. //execute the SQL query {create user <user name>@localhost identified by <password>}

  33.         mysql_query(connection,addUser);

  34. //puts(addUser);

  35.  

  36.  

  37. //creating another SQL query in the String

  38.         strcat(grantACC,"grant SELECT on ");

  39.         strcat(grantACC,database);

  40.         strcat(grantACC,".* to ");

  41.         strcat(grantACC,name);

  42.         strcat(grantACC,"@localhost");

  43. //execute the SQL query { grant SELECT on <database name>.* to <user name>@localhost }

  44.         mysql_query(connection,grantACC);

  45. //puts(grantACC);

  46.  

  47.  

  48. //closing the connection"User has been created Successfully

  49. ");

  50.  

  51. }

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions