954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

access denied!

i am starting to learn to connect a cprogram to a mysql database.

i have got this elementary program to connect to server , create a database and disconnect from it.

#include <stdio.h>
#include<windows.h>
#include <mysql.h>
static char *opt_host_name = NULL; /* server host (default=localhost) */
static char *opt_user_name =NULL; /* username (default=login name) */
static char *opt_password = NULL; /* password (default=none) */
static unsigned int opt_port_num = 0; /* port number (use built-in value) */
static char *opt_socket_name = NULL; /* socket name (use built-in value) */
static char *opt_db_name = NULL; /* database name (default=none) */
static unsigned int opt_flags = 0; /* connection flags (none) */
static MYSQL *conn; /* pointer to connection handler */
int
main (int argc, char *argv[])
{
/* initialize connection handler */
conn = mysql_init (NULL);
if (conn == NULL)
{
fprintf ( stderr,"mysql_init() failed (probably out of memory)\n");
exit (1);
}
/* connect to server */
if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
{
fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
mysql_errno (conn), mysql_error (conn));
mysql_close (conn);
exit (1);
}

//create database
if (mysql_query(conn, "create database firstdata")) {
      printf("xxxError %u: %s\n", mysql_errno(conn), mysql_error(conn));
      exit(1);
  }

/* disconnect from server */
mysql_close (conn);
exit (0);
}


but, when i execute it there is error message

access denied for user ''@'localhost'to database firstdata.


please help!

joy39
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Set the right username and password.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
Set the right username and password.

do, i have to first create a user in the command line using "create user" and then set this username and password in this code.
or, if i write the username and password in the code only and it will do the rest?

please help! i am pretty new at this!

joy39
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
create a user in the command line using "create user" and then set this username and password in this code.

Exactly.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You