MySQL API error

Write your question here.

I am compile .below C program utilizing the MySQL C API
After compile, I found that below error message.

8 1 C:\Users\patch\Documents\main.cpp [Error] expected initializer before 'int'
28 C:\Users\patch\Documents\Makefile.win recipe for target 'main.o' failed

How to proceed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <C:\Program Files\MySQL\MySQL Server 5.7\include/mysql.h>


int main()
int res(const char *s)
//int system(const char *s)
{
  MYSQL *m;
  MYSQL_RES *r;
  MYSQL_ROW w;
  MYSQL_STMT *s;
  MYSQL_BIND par[2];
  MYSQL_BIND res[2];
  char sd[4][60];
  unsigned long sdl[4];
  my_bool nl[4];
  char sql1[]= "SELECT user, host FROM mysql.user WHERE host LIKE '%localhost%'";
  char sql2[]= "SELECT user, host FROM mysql.user WHERE host LIKE ?";

  mysql_real_connect(m,"localhost","root","","test",0,NULL,0);

  puts("Regular Execution");
  mysql_real_query(m, sql1, strlen(sql1));

  r= mysql_store_result(m);
  while (w= mysql_fetch_row(r))
  {
    printf("%s@%s\n", w[0], w[1]);
  }

  puts("Prepared Statement");
  mysql_stmt_prepare(s, sql2, strlen(sql2));
  // parameter
  par[0].buffer_type= MYSQL_TYPE_STRING;
  par[0].buffer= sd[0];
  par[0].is_null= 0;
  par[0].length= &sdl[0];
  mysql_stmt_bind_param(s,par);
  

  strncpy(sd[0],"%localhost%",60);
  sdl[0]= strlen(sd[0]);
  mysql_stmt_execute(s);
  res[0].buffer_type= MYSQL_TYPE_STRING;
  res[0].buffer= sd[1];
  res[0].buffer_length= 60;
  res[0].is_null= &nl[1];
  res[0].length= &sdl[1];
  res[1].buffer_type= MYSQL_TYPE_STRING;
  res[1].buffer= sd[2];
  res[1].buffer_length= 60;
  res[1].is_null= &nl[2];
  res[1].length= &sdl[2];
  mysql_stmt_bind_result(s,res);

  while(w= mysql_fetch_row(r))
  {
    printf("%s@%s\n", sd[1], sd[2]);
  }

  mysql_stmt_free_result(s);
  mysql_stmt_close(s);
  mysql_close(m); 
You have a function res inside main, which is not allowed. Also main must start with {
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <C:\Program Files\MySQL\MySQL Server 5.7\include/mysql.h>

// declaration or prototype
int res(const char *s);

int main()
{
  res("Some Value");

  return 0;
}

//definition or implementation

int res(const char *s)
//int system(const char *s)
{
  MYSQL *m;
  MYSQL_RES *r;
  MYSQL_ROW w;
  MYSQL_STMT *s;
  MYSQL_BIND par[2];
  MYSQL_BIND res[2];
  char sd[4][60];
  unsigned long sdl[4];
  my_bool nl[4];
  char sql1[]= "SELECT user, host FROM mysql.user WHERE host LIKE '%localhost%'";
  char sql2[]= "SELECT user, host FROM mysql.user WHERE host LIKE ?";

  mysql_real_connect(m,"localhost","root","","test",0,NULL,0);

  puts("Regular Execution");
  mysql_real_query(m, sql1, strlen(sql1));

  r= mysql_store_result(m);
  while (w= mysql_fetch_row(r))
  {
    printf("%s@%s\n", w[0], w[1]);
  }

  puts("Prepared Statement");
  mysql_stmt_prepare(s, sql2, strlen(sql2));
  // parameter
  par[0].buffer_type= MYSQL_TYPE_STRING;
  par[0].buffer= sd[0];
  par[0].is_null= 0;
  par[0].length= &sdl[0];
  mysql_stmt_bind_param(s,par);
  

  strncpy(sd[0],"%localhost%",60);
  sdl[0]= strlen(sd[0]);
  mysql_stmt_execute(s);
  res[0].buffer_type= MYSQL_TYPE_STRING;
  res[0].buffer= sd[1];
  res[0].buffer_length= 60;
  res[0].is_null= &nl[1];
  res[0].length= &sdl[1];
  res[1].buffer_type= MYSQL_TYPE_STRING;
  res[1].buffer= sd[2];
  res[1].buffer_length= 60;
  res[1].is_null= &nl[2];
  res[1].length= &sdl[2];
  mysql_stmt_bind_result(s,res);

  while(w= mysql_fetch_row(r))
  {
    printf("%s@%s\n", sd[1], sd[2]);
  }

  mysql_stmt_free_result(s);
  mysql_stmt_close(s);
  mysql_close(m); 
}
Topic archived. No new replies allowed.