开发者

Access deny Error in C and MySql on Ubuntu

Hi I'm trying to connect MySql using C in my project and it's the first time I use C and MySql. I try to save a user name, password and role in MySql. When I run the program like that it's ok.

 #include <mysql.h>
 #include <stdio.h>

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

char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
char query[500];
memset(query,0,500);
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);
}
int i =2;
/* send SQL query */
char userName[45] ="hi";
char userPassword[45];
char role[45];
strcpy(userName,"koko");
strcpy(userPassword,"hi");
strcpy(role,"admin");

sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",userName,userPassword,role);

if (mysql_query(conn, query)) {
  fprintf(stderr, "%s\n", mysql_error(conn));
  exit(2);
}


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

I can run it without any errors though there are some warnings. But when i wrote this program by using function, I got this error when I compile it . Access denied for user 'root'@'localhost' (using password: YES)

#include <mysql.h>
#include <stdio.h>


MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
void saveUser(char * , c开发者_高级运维har * ,char * );
void updateUser(char * , char * ,char * );
void showData();
main() 
{
saveUser("kevin","hi","admin");
}

void saveUser(char *name, char *password,char *role)
{

char  query[500];
memset(query,0,500);
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);
}
int i =2;
/* send SQL query */
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",name,password,role);
if (mysql_query(conn, query)) {
  fprintf(stderr, "%s\n", mysql_error(conn));
  exit(2);
}

res = mysql_use_result(conn);



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

void updateUser(char *name, char *password,char *role)
{

char  query[500];
memset(query,0,500);
conn = mysql_init(NULL);

/* Connect to database */
if (!mysql_real_connect(conn, server,
     user, password, database, 0, NULL, 0)) {
  printf("1234511\n");
  fprintf(stderr, "%s\n", mysql_error(conn));
  exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"update userTbl set password=\'%s\',role=\'%s\' where name=\'%s\'; ",password,role,name);
if (mysql_query(conn, query)) {
  fprintf(stderr, "%s\n", mysql_error(conn));
  exit(2);
}


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

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

Can anybody explain me where I went wrong and how can I correct it. I use gcc to get the executable file and run it on command prompt. Thanks in advance.

Kevin


i read your post it's not the problem with your code try to connect to mysql server with the parameters(username and password) using a mysql client software like mysql query browser or sql yog or phpmyadmin whatever.

i think you set your password to blank, so simply replace the line

saveUser("kevin","hi","admin");

to

saveUser("kevin","","admin");

also ensure that you are passing the parameters in correct case since ubuntu's behavior is case senstive

give it a try if it helps.

please check for your correct username and passwords. usually when you install mysql server it asks for password and user to be set. as you are using ubuntu you might be using lamp server so try to get the correct credentials

there only the problem is with your credentials


Hi both Simon and Devjosh , Thanks for you help. I found the problem._The global variable name password is the same with the parameter name password in saveUser function _. Thanks a lot and so sorry disturb you .

Kevin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜