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!

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!

create a user in the command line using "create user" and then set this username and password in this code.

Exactly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.