0

I'm coding a setup for my "game" server and I use MySQL for database. I getting an error when I create the database + table and the columns. I tested the Mysql code in phpmyadmin and it worked.

Here's a sample of my code: `

int area;
char user[100];
char pass[100];
char ip[200];
char tentativaspm;
void mysqlsetup(){


  MYSQL *CON = mysql_init(NULL);
  if (CON == NULL)
  {
    fprintf(stderr, "%s\n", mysql_error(CON));
    exit(1);

  }
  if (mysql_real_connect(CON, ip, user, pass,
          NULL, 0, NULL, 0) == NULL)
  {
      fprintf(stderr, "%s\n", mysql_error(CON));
      mysql_close(CON);
      sleep(2);
      userepassmysql();
  }
  printf("Conecao establecida.\n");
  if(mysql_query(CON, "CREATE DATABASE place;")){
      fprintf(stderr, "%s\n", mysql_error(CON));
      mysql_close(CON);
      exit(1);


  }


if(mysql_query(CON, "USE place; CREATE TABLE grid (pixelID int,color int);"){
      fprintf(stderr, "%s\n", mysql_error(CON));
      mysql_close(CON);
      exit(1);

}

And I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USE grid CREATE TABLE grid (pixelID int(11) NOT NULL,color int(11) NOT N' at line 1

1 Answer 1

2

By default the mysql_query c function doesn't support multiple SQL statements. But you can combine the two queries

USE [database];
CREATE TABLE grid (pixelID int,color int)

into the oneliner

CREATE TABLE [database].grid (pixelID INT, color INT) 

Note replace [database] with the correct database name.

Solution one

Using the oneliner

if(mysql_query(CON, "CREATE TABLE place.grid (pixelID int,color int);"){

Solution two

Use the function mysql_set_server_option() function to enable multiple SQL statements.

mysql_set_server_option(connection, MYSQL_OPTION_MULTI_STATEMENTS_ON);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :D Have a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.