Click here to Skip to main content
15,501,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 i need detailed code with comment line description as well.
Mohibur Rashid 15-Nov-11 23:39pm    
Did you search on google yet????
OriginalGriff 16-Nov-11 2:17am     CRLF
The OP paosted as a new question: "yes but yadav has given only how to connect the database using c but i need only insert operation along with the database connection as well"


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

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;
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));
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
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 */

Hope this Helps.
Share this answer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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