int artist_id = -1;
/* Он уже существует? */
mysql_escape string(es, artist, strlen(artist));
sprintf(qs, "SELECT id FROM artist WHERE name = '%s'", es);
res = mysql_query(&my_connection, qs);
if (res) {
fprintf(stderr, "SELECT error: %s\n", mysql_error(&my_connection));
} else {
res_ptr = mysql_store_result(&my_connection);
if (res_ptr) {
if (mysqr_num_rows(res_ptr) > 0) {
if (mysqlrow = mysql_fetch_row(res_ptr)) {
sscanf(mysqlrow[0], "%d", &artist_id);
}
}
mysql_free_result(res_ptr);
}
}
if (artist_id != -1) return artist_id;
sprintf(is, "INSERT INTO artist(name) VALUES ('%s')", es);
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) {
printf("SELECT error: %s\n", mysql_error(&my_connection));
return 0;