Click here to Skip to main content
15,887,364 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database. this is the actual question please send everything in detail along with the description.
Posted
Updated 16-Nov-11 3:54am
v2

Did you Googled for it?
For instance, "MySQL C API programming tutorial"[^] has everything you need about.
 
Share this answer
 
For the MySQL syntax.

To add a new user use CREATE USER Syntax[^]

To grant access to do anything other then be able to login.

Use GRANT Syntax[^]


For the MySQL connector for C, download the connector her http://dev.mysql.com/downloads/connector/c/[^]
 
Share this answer
 
Hi,

Here are requests to be executed to insert your special user in response to your request :

SQL
CREATE 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT INSERT ON database.* TO 'newuser'@'localhost';


Here is the code to manage connection and request execution to database, basic stuff :
#include <mysql.h>
#include <stdio.h>

main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *server = "localhost";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "mysql";

conn = mysql_init(NULL);

/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

res = mysql_use_result(conn);

/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);

/* close connection */
mysql_free_result(res);
mysql_close(conn);
}



Hope this Helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900