| Exolent[jNr] |
06-13-2011 13:13 |
Re: Reading ini file through sql table
These 2 functions may help:
PHP Code:
public sql_load() { new Handle:tuple = SQL_MakeStdTuple(); if(tuple == Empty_Handle) return; new errcode, error[128]; new Handle:db = SQL_Connect(tuple, errcode, error, charsmax(error)); if(db == Empty_Handle) { log_amx("%s", error); return; } new Handle:query = SQL_PrepareQuery(db, "CREATE TABLE IF NOT EXISTS ads \ (message VARCHAR(128) NOT NULL, \ autorespond VARCHAR(32) NOT NULL DEFAULT '', \ condition_map VARCHAR(32) NOT NULL DEFAULT '', \ condition_minplayers VARCHAR(32) NOT NULL DEFAULT '', \ condition_maxplayers VARCHAR(32) NOT NULL DEFAULT '');"); if(!SQL_Execute(query)) { SQL_QueryError(query, error, charsmax(error)); log_amx("Error creating table: %s", error); } SQL_FreeHandle(query); query = SQL_PrepareQuery(db, "SELECT message, autorespond, condition_map, condition_minplayers, condition_maxplayers FROM ads;"); if(!SQL_Execute(query)) { SQL_QueryError(query, error, charsmax(error)); log_amx("Error selecting ads: %s", error); } else if(SQL_NumResults(query)) { new message[128], said[32], type, i, condition[32]; while(SQL_MoreResults(query)) { SQL_ReadResult(query, 0, message, charsmax(message)); SQL_ReadResult(query, 1, said, charsmax(said)); type = said[0] ? SAY_AD : NORM_AD; for(i = 0; i < 3; i++) { SQL_ReadResult(query, (i + 2), condition, charsmax(condition)); if(!condition[0]) continue; setString(COND, type, condition, adCount[type], i); } setColor(message, charsmax(message)); if(type == SAY_AD) { setString(STORE, SAY_AD, said, adCount[SAY_AD], 0); setString(STORE, SAY_AD, message, adCount[SAY_AD], 1); } else setString(STORE, NORM_AD, message, adCount[NORM_AD]); adCount[type]++; SQL_NextRow(query); } } SQL_FreeHandle(query); SQL_FreeHandle(db); //Set a first task, if there are any normal ads if(adCount[NORM_AD] != 0) set_task(random_float(RAND_MIN, RAND_MAX), "eventTask"); }
public ini_to_sql() { new Handle:tuple = SQL_MakeStdTuple(); if(tuple == Empty_Handle) return; new errcode, error[128]; new Handle:db = SQL_Connect(tuple, errcode, error, charsmax(error)); if(db == Empty_Handle) { log_amx("%s", error); return; } new Handle:query = SQL_PrepareQuery(db, "DELETE FROM ads;"); if(!SQL_Execute(query)) { SQL_QueryError(query, error, charsmax(error)); log_amx("Error deleting all ads: %s", error); } SQL_FreeHandle(query); //Load the data new filepath[64]; get_configsdir(filepath, 63); format(filepath, 63, "%s/advertisements.ini", filepath); if(file_exists(filepath)) { new output[512], conditions[128], temp[64], type; //Open file new fHandle = fopen(filepath, "rt"); //Checks for failure if(!fHandle) return; //Loop through all lines for(new a = 0; a < MAXADS && !feof(fHandle); a++) { //Get line fgets(fHandle, output, 511); //Work away comments if(output[0] == ';' || !output[0] || output[0] == ' ' || output[0] == 10) { //Line is not counted a--; continue; } //Reset type type = 0; new map[32], minplayers[32], maxplayers[32], said[32]; //Check if it contains conditions if(output[0] == COND_TKN) { //Cut the conditions off the string split(output, conditions, 127, output, 511, DEVIDE_STKN); //Determine if its say check or normal ad type = output[0] == SAY_TKN ? 1 : 0; //Put the conditions in own space for(new b = 0; b < 3; b++) { new sort[16], cond[32], numb; //Remove the % from line conditions[0] = ' '; trim(conditions); //Get one condition from the line split(conditions, temp, 64, conditions, 127, COND_STKN); split(temp, sort, 15, cond, 31, " "); if(equali(sort, "map")) { copy(map, 31, cond); } else if(equali(sort, "min_players")) { copy(minplayers, 31, cond); } else if(equali(sort, "max_players")) { copy(maxplayers, 31, cond); } else { continue; } //Exit if it hasn't got more conditions if(!conditions[0]) break; } } if(type == 0) type = output[0] == SAY_TKN ? 1 : 0; if(type == SAY_AD) { //Remove the @ from line output[0] = ' '; trim(output); split(output, said, 31, output, 127, DEVIDE_STKN); } query = SQL_PrepareQuery(db, "INSERT INTO ads \ (message, autorespond, condition_map, condition_minplayers, condition_maxplayers) \ VALUES \ (^"%s^", ^"%s^", ^"%s^", ^"%s^", ^"%s^");", output, said, map, minplayers, maxplayers); if(!SQL_Execute(query)) { SQL_QueryError(query, output, charsmax(output)); log_amx("Error inserting ad: %s", output); } SQL_FreeHandle(query); } //Close file to prevent lockup fclose(fHandle); } SQL_FreeHandle(db); }
|