Example #1
0
int add_cd(char* artist, char* title, char* catalogue, int *cd_id){
    MYSQL_RES* pRes;
    MYSQL_ROW rowData;
    int res, artist_id = -1, new_cd_id = -1;
    char is[250], es[250];
    if (!connected) {
        return 0;
    }

    artist_id = get_artist_id(artist);
    mysql_escape_string(es, title, strlen(title));
    sprintf(is, c2s("insert into cd(title,artist_id, catalogue) values('%s', '%d', '%s')"), es, artist_id, catalogue);
    res = mysql_query(&mysql, is);
    if (res){
        fprintf(stderr, c2s("Insert error %d: %s\n"), MS_ENO(&mysql), MS_EMSG(&mysql));
        return 0;
    }

    res = mysql_query(&mysql, c2s("select last_insert_id()"));
    if (res){
        fprintf(stderr, c2s("Select error %d: %s\n"), MS_ENO(&mysql), MS_EMSG(&mysql));
        return 0;
    } else {
        pRes = mysql_use_result(&mysql);
        if (pRes){
            if ((rowData = mysql_fetch_row(pRes))){
                sscanf(rowData[0], "%d", &new_cd_id);
            }
            mysql_free_result(pRes);
        }
    }
    *cd_id = new_cd_id;
    if (new_cd_id != -1) return 1;
    return 0;
}
int add_cd(char* artist,char* title,char* catalogue,int* cd_id)
{
	MYSQL_RES *res_ptr;
	MYSQL_ROW mysqlrow;
	int res;
	char is[250];
	char es[250];

	int artist_id = -1;
	int new_cd_id = -1;
	if(!dbconnected)
	{
		return 0 ;
	}

	artist_id = get_artist_id(artist);
	mysql_escape_string(es,title,strlen(title));
	sprintf(is,"INSERT INTO cd(title,artist_id,catalogue) VALUES('%s',%d,'%s')",es,artist_id,catalogue);
	res = mysql_query(&my_connection,is);
	if(res)
	{
		fprintf(stderr,"Insert error:%d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
		return 0 ;
	}
	res = mysql_query(&my_connection,"SELECT LAST_INSERT_ID()");
	if(res)
	{
		fprintf(stderr,"SELECT error:%d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
		return 0 ;
	}
	else
	{
		res_ptr = mysql_use_result(&my_connection);
		if(res_ptr)
		{
			mysqlrow = mysql_fetch_row(res_ptr);
			if(mysqlrow)
			{
				sscanf(mysqlrow[0],"%d",&new_cd_id);
			}
			mysql_free_result(res_ptr);
		}
		*cd_id = new_cd_id;
		if(-1 != new_cd_id )
		{
			return 1;
		}
		return 0 ;
	}
}
/* Add a cd. Return 1 if successful, if there are any failures.
 * set the address of cd_id to be the id of the cd we added (which is
 * auto-incremented in the database).
 */
int add_cd(char *artist, char *title, char *catalogue, int *cd_id) {
    MYSQL_RES *res_ptr;
    MYSQL_ROW mysqlrow;
    int res;
    char is[250];  // insert string
    char es[250];  // escaped string - we escape all string inputs!
    int artist_id = -1;
    int new_cd_id = -1;

    if (!dbconnected) return 0;

    // handle the artist_id. This might do a lookup or an insert, depending.
    artist_id = get_artist_id(artist);

    // save the escaped title in es, the constructed query in is.
    // Then run the query and check the return code.
    mysql_escape_string(es, title, strlen(title));
    sprintf(
        is,
        "INSERT INTO cd(title, artist_id, catalogue) VALUES('%s', %d, '%s')",
        es, artist_id, catalogue
    );
    res = mysql_query(&my_connection, is);
    if (res) {
        fprintf(stderr, "Insert error %d: %s\n",
                mysql_errno(&my_connection), mysql_error(&my_connection));
        return 0;
    }

    // now, get the id, which we 'return' to user via the *cd_id input
    res = mysql_query(&my_connection, "SELECT LAST_INSERT_ID()");
    if (res) {
        printf("SELECT error: %s\n", mysql_error(&my_connection));
        return 0;
    }
    res_ptr = mysql_use_result(&my_connection);
    if (res_ptr) {
        if ((mysqlrow = mysql_fetch_row(res_ptr))) {
            sscanf(mysqlrow[0], "%d", &new_cd_id);
        }
        mysql_free_result(res_ptr); // remember to free result!
    }
    *cd_id = new_cd_id;

    // return 1 if everything worked (id is set and nonzero), 0 otherwise
    if (new_cd_id != -1) return 1;
    return 0;
}
Example #4
0
/*----------------------------------------*/
int add_cd(char *artist, char *title, char *catalogue, int *cd_id)
{
   MYSQL_RES *res_ptr;
   MYSQL_ROW mysqlrow;
   
   int res;
   char is[100];
   char es[100];
   int artist_id = -1;
   int new_cd_id = -1;
   
   if(!dbconnected) return 0; /* if there is no connection */
   artist_id = get_artist_id(artist); /* a call to another function */
   
   mysql_escape_string(es, title, strlen(title));
   /* the above returns es as title and below es is used to store query */

   sprintf(is, 
   "INSERT INTO cd(title, artist_id, catalogue) VALUES ('%s', %d, '%s')", 
   es, artist_id, catalogue); 

   res = mysql_query(&my_connection, is);
   if(res)  {
       fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&my_connection),
        mysql_error(&my_connection));
        return 0;
   }

   /* obtain id for the last query */
   res = mysql_query(&my_connection, "SELECT LAST_INSERT_ID()");
   if(res)  {
       printf("SELECT error: %s\n", mysql_error(&my_connection));
       return 0;
   }
   else {
       res_ptr = mysql_use_result(&my_connection);
       if(res_ptr) {
           if((mysqlrow = mysql_fetch_row(res_ptr)))
               sscanf(mysqlrow[0],"%d", &new_cd_id);
           mysql_free_result(res_ptr);   
       }
       *cd_id = new_cd_id;
       if(new_cd_id != -1) return 1;
       return 0;
   }
}