vishnu raju
Answered On : Mar 13th, 2012
try this :)
Code
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char name[10];
char password[10];
char database[10];
char addUser[60]="";
char grantACC[60]="";
// initialize the variables within
MYSQL *connection=mysql_init(NULL); //MYSQL is structure represents a handle to one database connection
// connect to the database
if (!mysql_real_connect(connection,"localhost","root","mysql","clanguage", 0, NULL, 0)) {
printf("in the if block");
printf("Conection error : %s
", mysql_error(connection)); //For print error if connection is not stablished
exit(1);
}
printf("connection established successfully
");
//retrieve User name from STDIN
printf("Enter the User Name
");
scanf("%s",name);
//retrieve Password from STDIN
printf("Enter the Password
");
scanf("%s",password);
//retrieve database name from STDIN
printf("Enter the Database Name
");
scanf("%s",database);
//creating SQL query in the form String
strcat(addUser,"create user ");
strcat(addUser,name);
strcat(addUser,"@localhost identified by ");
strcat(addUser,password);
strcat(addUser,"");
//execute the SQL query {create user <user name>@localhost identified by <password>}
mysql_query(connection,addUser);
//puts(addUser);
//creating another SQL query in the String
strcat(grantACC,"grant SELECT on ");
strcat(grantACC,database);
strcat(grantACC,".* to ");
strcat(grantACC,name);
strcat(grantACC,"@localhost");
//execute the SQL query { grant SELECT on <database name>.* to <user name>@localhost }
mysql_query(connection,grantACC);
//puts(grantACC);
//closing the connection
mysql_close(connection);
printf("User has been created Successfully
");
}

1 User has rated as useful.
Login to rate this answer.
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.